Using Regex for spaces
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
22 Replies
⌛
This post has been reserved for your question.
Hey @bed! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
You can use
\s
(any whitespace) or similar for spaces
or just normal spaces to only match thatScanner (Java SE 21 & JDK 21)
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 delimitersalr ty
lemme check if it works rq
yeah
yeah thats fine
how come this doesn't work

As I said, hasNext stops at \s by default
wdym
im not following
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 delimiterohhh
wait 1 second
let me test smth
eesh
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 regexoh
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.alr
tysm
np
Post Closed
This post has been closed by <@1167522887947796590>.