1

I tried doing something like what these people have done: How to split a comma-separated string? But when I do the following:

String UserInput = new Scanner(System.in).next();
GradesNonInt = Arrays.asList(UserInput.replaceAll("\\s","").split(","));
System.out.println(GradesNonInt.size());

When I input a string like "1,2,3" I get 3 printed. When I type "1, 2, 3" I get 1 printed. For some reason, it does not want to work when the string has spaces in it.

As one of the answers below said, entering "1, 2, 3" directly seems to work fine, but yet when taken from the Scanner(System.in).next(), it is not.

Community
  • 1
  • 1

2 Answers2

2

I tried following code and it printed 3 when I used "1, 2, 3" in UserInput string

    String UserInput = "1, 2, 3";
    List<String> GradesNonInt = Arrays.asList(UserInput.replaceAll("\\s","").split(","));
    System.out.println(GradesNonInt.size());

The issue is not with split but this is happening cause Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace and that's why this behaviour is shown. Refer this link.

If you want you can change the delimiter to something else than space by using useDelimiter method of Scanner class


UPDATE

To change the delimiter to next line and to ensure the code accept spaces in the strings, you can change your code to following

    String UserInput = new Scanner(System.in).useDelimiter("\n").next();
    List<String> GradesNonInt = Arrays.asList(UserInput.replaceAll("\\s","").split(","));
    System.out.println(GradesNonInt.size());
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
  • Thank, I hadn't actually realized that the UserInput might be the problem. I will edit the OP. – Joel Hemphill Oct 01 '15 at 00:16
  • Which leads to conclusion that problem lies in `new Scanner(System.in).next();`... – Pshemo Oct 01 '15 at 00:16
  • @JoelHemphill If this issue is solved, then you can accept the answer which helped you solved this query – Balwinder Singh Oct 01 '15 at 00:31
  • `useDelimiter("\n")` is bad style. We already have `nextLine()` method for that. Also it can cause a lot of problems if we use this code on OS which is using only `\r`. It will also prevent us from such Scanner's `hasNextInt` method with data in form `1 2 3`. – Pshemo Oct 01 '15 at 00:36
  • @Pshemo I understand that part. Just wanted to show the use of delimiter. He can choose any other delimiter based on his needs or even the `nextLine()` method – Balwinder Singh Oct 01 '15 at 00:42
  • Fair enough, but I will leave that comment to show potential problems with `\n` delimiter. – Pshemo Oct 01 '15 at 00:43
1

Problem is that Scanner#next() can return only next token (until it finds next delimiter, or end of data). So for input like 1, 2, 3, it will return 1,.

Use Scanner#nextLine() to read entire line.

Also don't create new instance of Scanner each time you want to read data from user. Create one Scanner handling System.in per application and reuse it.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
Pshemo
  • 122,468
  • 25
  • 185
  • 269