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

gymcode
  • 3,955
  • 13
  • 55
  • 110

2 Answers2

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.

Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • 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 – gymcode Feb 09 '15 at 12:37
  • Having some trouble finding this information with my good friend Google. Is the default delimiter for `next()` just an empty space? – Drew Kennedy Feb 09 '15 at 12:41
  • I suspect that you used `nextInt()` or other form of `nextXXX()` method before `nextLine()`. You can find solution for this problem here http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint – Pshemo Feb 09 '15 at 12:42
  • 1
    @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). – Pshemo Feb 09 '15 at 12:45
  • @Pshemo Perfect. Thanks! – Drew Kennedy Feb 09 '15 at 12:46
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()

Stultuske
  • 8,465
  • 1
  • 19
  • 30
  • 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 – gymcode Feb 09 '15 at 12:37