I have taken an input from stdin. This input will be a line of several numbers. For example a valid value for the variable line will be :
1 2 3 4 5 6 7 8 9 10
I know how many numbers there will be and I’ve stored this in the variable N. I am trying to store these numbers in an array of size N.
String a="";
for(int i=0; i<line.length(); i++){
if(line.charAt(i)!=' ')
a = a+ line.charAt(i);
else{
numbers[x++]=Integer.parseInt(a);
a="";
}
}
numbers[x]=Integer.parseInt(a); //to store the last number in the array
Is there a more efficient way of doing this ?
1 Answer