#Using Regex for spaces

45 messages · Page 1 of 1 (latest)

brave anchor
#

Hello, I currently use !hasnext() to try and ensure that the input word doesn't have any characters other than letters, which works fine
however I am not sure how to make it so that regex also detects whether or not any spaces are in the input

winter pelicanBOT
#

This post has been reserved for your question.

Hey @brave anchor! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mortal pollen
#

You can use \s (any whitespace) or similar for spaces

#

or just normal spaces to only match that

#

Note that by default, hasNext/next only checks one token

#

so it doesn't go over multiple spaces

#

you can use useDelimiter to change that behavior

#

If you want to check it per line, you can set line breaks as delimiters

brave anchor
#

alr ty

#

lemme check if it works rq

brave anchor
#

how come this doesn't work

mortal pollen
brave anchor
#

wdym

#

im not following

brave anchor
#

the input string has multiples spaces

#

but "No!" still doesnt run

mortal pollen
#

if you have an input like Hello World, hasNext/next only sees the Hello

#

unless you have a custom delimiter

brave anchor
#

ohhh

#

wait 1 second

#

let me test smth

#

eesh

mortal pollen
#

hasNext/next only sees one token but you can configure what is used to split tokens

brave anchor
#

how would i make it only take that first word then?

#

like if hello word was input

#

how would i make it just take hello

mortal pollen
#

hasNext()/next() literally does that by default without any regex

brave anchor
#

oh

#

1 sec again

#

right

#

i see what u mean

#

well that solves my issue entirely ngl

winter pelicanBOT
# brave anchor well that solves my issue entirely ngl

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

mortal pollen
#

nextLine --> read entire line
next --> read one token (word)

mortal pollen
#

but you cannot just use nextLine after next

winter pelicanBOT
#

If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.

brave anchor
#

alr

#

tysm

mortal pollen
#

np