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)
2 odpowiedzi
+ 5
String num[] = new java.util.Scanner(System.in).nextLine().trim().split(",");
for(String i:num){
System.out.println((int)Integer.valueOf(i));}
+ 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){}