Getting NumberFormatException. What am I doing wrong here ? I'm trying to read an array of integers separated by spaces. Is there any other better way to do the same ? I did a lot of research and most people said that split() is better than StringTokenizer and BufferedReader.
public static void main (String args[])
{
@SuppressWarnings("resource")
Scanner io = new Scanner(System.in);
int a=io.nextInt();
String input;
for(int i=0; i<a; i++)
{
input = io.nextLine();
int x= input.length();
String[] splitArr = input.split("\\s+");
int p[]= new int[x];
int q=0;
for (String par : splitArr) {
p[q++] = Integer.parseInt(par);}
System.out.println(p);
}
}
}
Updated Code, working now:
public static void main (String args[])
{
@SuppressWarnings("resource")
Scanner io = new Scanner(System.in);
int a=io.nextInt();
io.nextLine();
String input;
for(int i=0; i<a; i++)
{
int x= io.nextInt(); //size of array
io.nextLine();
input = io.nextLine();
String[] splitArr = input.split("\\s+");
int p[]= new int[x];
int q=0;
for (String par : splitArr) {
p[q++] = Integer.parseInt(par);}
for(int m=0; m<x; m++)
System.out.print(p[m]+" ");
}
}
}