1

I would like to prevent user from includingSpace in a String input.

I have tried using a few methods such as:

(team1Name.contains(" "))

(team1Name.matches(".*([ \t]).*"))

(team1Name.indexOf(' ') >= 0) but to no avail.


Below is a a snippet of my code and the output:

Code snippet:

System.out.print("Name of Team 1: ");
team1Name = sc.next();

if (team1Name.indexOf(' ') >= 0) {
    System.err.println("Error");
    System.out.print("Name of Team 1: ");
    team1Name = sc.next();
}

System.out.print(team1Name+ " Goals: ");

while (true) {
    try {
        team1Goals = Integer.parseInt(sc.next());
        break;
      } catch (NumberFormatException nfe) {
        System.err.println("Error");
        System.out.print(team1Name+ " Goals: ");
      }
}

Output:

Name of Team 1: black sheep 
Error 
black Goals: black Goals:

UPDATE:

Attempt to use .nextLine() instead of .next(). However, still receive error output:

Name of Team 1: black sheep
Error
Error
[] Goals: [] Goals: 

Placed [] to replace the original Space/empty output

0

2 Answers 2

2

Scanner#next() will not return string with space because space is considered as delimiter, so for input like black sheep

  • first invocation of next() fill return black
  • and another invocation of next() will return sheep.

Since result of your second next() invocation is used as argument in Integer.parseInt(...) you are getting NumberFormatException because this method can't parse sheep.

Consider instead next() using nextLine() to read entire string from line.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you I changed my code to usage of .nextLine() instead of .next(). This time a different situation occured. It did not take in my input with Space, and it still jumps to the next print
Having some trouble finding this information with my good friend Google. Is the default delimiter for next() just an empty space?
I suspect that you used nextInt() or other form of nextXXX() method before nextLine(). You can find solution for this problem here stackoverflow.com/questions/13102045/…
@DrewKennedy No, space is not the only delimiter. If you will print System.out.println(new Scanner("").delimiter()); you will see that its delimiter is regex \p{javaWhitespace}+ so it uses one or more whitespaces (as whitespace we can also treat \n \r \t and few other characters).
1

This is because you use the next method of Scanner, which only takes the first token, in this case: black. This obviously does not contain a space. If you want the entire input, use nextLine()

1 Comment

Thank you I changed my code to usage of .nextLine() instead of .next(). This time a different situation occured. It did not take in my input with Space, and it still jumps to the next print

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.