How to use java split() function in a loop? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use java split() function in a loop?

I'm working on a program that takes in input from a user. The input should be comma separated like 5 // number of points to input 2,3 3,4 5,6 14,9 6,7 How can I do this using the split() function in java? (It should also convert to integers)

13th Aug 2019, 3:15 PM
Noobs
Noobs - avatar
2 Answers
+ 5
String num[] = new java.util.Scanner(System.in).nextLine().trim().split(","); for(String i:num){ System.out.println((int)Integer.valueOf(i));}
13th Aug 2019, 5:33 PM
D_Stark
D_Stark - avatar
+ 1
without split: Scanner sc = new Scanner("2,3\n3,4\n5,6\n14,9\n6,7"); //System.in); sc.useDelimiter("[,\\s]"); try { while(true) System.out.println(sc.nextInt() ); } catch (NoSuchElementException e){}
13th Aug 2019, 6:52 PM
zemiak