#Using Regex for spaces
45 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @brave anchor! Please use
/closeor theClose Postbutton 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.
You can use \s (any whitespace) or similar for spaces
or just normal spaces to only match that
I assume you are using the Scanner#hasNext(Pattern) method?
declaration: module: java.base, package: java.util, class: Scanner
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
yeah
yeah thats fine
how come this doesn't work
As I said, hasNext stops at \s by default
by this you mean that it only check to see if there is at least one space right?
the input string has multiples spaces
but "No!" still doesnt run
if you have an input like Hello World, hasNext/next only sees the Hello
unless you have a custom delimiter
hasNext/next only sees one token but you can configure what is used to split tokens
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
hasNext()/next() literally does that by default without any regex
oh
1 sec again
right
i see what u mean
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.
nextLine --> read entire line
next --> read one token (word)
alr thanks man
but you cannot just use nextLine after next
If Scanner#nextLine reads an empty line after calling a different Scanner#next method, take a look at this StackOverflow post.
np