0

Segment of Code Results in Exception

This segment of my code seems to result in an exception every time (even when it shouldn't): Scanner in = new Scanner(System.in); String s = in.nextLine().trim(); Scanner r = new Scanner(s).useDelimiter(","); int n = r.nextInt(); int d = r.nextInt(); Anyone know why?

25th Mar 2018, 7:44 AM
Nitro
Nitro - avatar
1 Answer
+ 1
My guess is you entered spaces in your string. ' 5 , 6 ' will generate an exception. ' 5,6 ' with zero spaces between the 5 and 6 works. Trim will remove leading and trailing spaces not those in the middle. Use .replaceAll("[ \t]", "") to remove the middle ones.
25th Mar 2018, 9:55 PM
John Wells
John Wells - avatar