0

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]+" ");
         }
    }

    }
  • 1
    The input part you are trying to parse is not an integer. – Lemonov Jun 29 '17 at 05:23
  • @Lemonov How to fix that ? The array of integers user enters will be a string so how to change that ? – Harshit Trehan Jun 29 '17 at 05:25
  • I don't see how we can really help you since we can't see your input string. Try doing `System.out.println(par)` just before you try to parse it, so that you can see just what string you're getting the error on. Or look at it in a debugger. – ajb Jun 29 '17 at 05:27
  • io.nextInt(); will give you next integers, call it as many times as you need :) or try catching this exception if you still think that one of paramters can still be other than number – Lemonov Jun 29 '17 at 05:27
  • What is the exact text that you entered when you got this error? – Erwin Bolwidt Jun 29 '17 at 05:28
  • I removed the outer for loop and after that i entered this- 1 3 5 2 6. The output was- [I@1f96302 – Harshit Trehan Jun 29 '17 at 05:31

2 Answers2

1

Do a io.nextLine(); (without storing it anywhere) after int a=io.nextInt(); (just before any string input that you'd want to take).

Something like

    Scanner io = new Scanner(System.in);
    int a=io.nextInt();
    io.nextLine();
    String input;

Loop through your array to get the values properly; not System.out.prinltn(p);

Aakash Verma
  • 2,459
  • 1
  • 18
  • 45
-1

Try doing something like this

    while(io.hasNext()){ 
        try{
            somevar = io.nextInt()
        }
        catch (NumberFormatException e){
            //just pass through this non int value or do some error handlin
        }
    }
Lemonov
  • 446
  • 4
  • 15