Java Issue while storing Integer Input as Array

时间:2018-02-03 09:03:44

标签: java arrays java.util.scanner arr

I am trying to write a program to which takes two inputs:

  • Size of the Array

  • Elements inside the array

e.g. Something which can pass the following test case:

5
0 1 3 4 5

The first line tells the size of the array i.e. 5 elements and the second line provides those 5 elements.

Further I need to store the values of those elements inside an Array.

Following is the code I tried:

    public static void main(String[] args) {

        Scanner scanner = new Scanner( System.in );

        System.out.print( "Enter Array Length: " );

        int input = scanner.nextInt();
        System.out.print( "Enter Array Elements: " );

        int array[]= {};
        String line1 = scanner.nextLine(); // Read 1st line
        String[] numbers1 = line1.split(" "); // Split based on space
        for(int i = 0; i<numbers1.length; i++){
            array[i] = Integer.parseInt(numbers1[i]);
        }
        System.out.print( "Array Print:  "+ Arrays.toString(array) );
   }

However, I keep getting "NumberFormatException" while calling parseInt for the same.

4 个答案:

答案 0 :(得分:0)

There are two mistakes.

  • Use nextLine() after nextInt()
  • Allot to sufficient memory for the integers being stored.

Test,

int input = scanner.nextInt();
scanner.nextLine();
System.out.print( "Enter Array Elements: " );

int array[]= new int[input];

答案 1 :(得分:0)

There are two mistakes:

1) Arrays are objects in java. So before using an array you need to create instance of that array.

2) Use next() instead of nextLine() because nextLine() does not read the current line of input but the next line which obviously is blank in your case. That's why you were getting a NumberFormatException.

public static void main(String[] args) {

        Scanner scanner = new Scanner( System.in );

        System.out.print( "Enter Array Length: " );

        int input = scanner.nextInt();
        System.out.print( "Enter Array Elements: " );

        int array[]= new int[input];
        String line1 = scanner.next(); // Read 1st line
        String[] numbers1 = line1.split(" "); // Split based on space
        for(int i = 0; i<numbers1.length; i++){
            array[i] = Integer.parseInt(numbers1[i]);
        }
        System.out.print( "Array Print:  "+ Arrays.toString(array) );
}

答案 2 :(得分:0)

You miss to defined size of array. You are taking size but not assigning in that case it will throw ArrayIndexOutofbound exception.

    Scanner scanner = new Scanner( System.in );

    System.out.print( "Enter Array Length: " );

    int input = scanner.nextInt();

    // use two scanners
    Scanner scanner2 = new Scanner( System.in );
    System.out.print( "Enter Array Elements: " );
    String line1 = scanner2.nextLine(); // Read 1st line
    int[] array = new int[input];

    String[] numbers1 = line1.split(" "); // Split based on space
    for(int i = 0; i<numbers1.length; i++){
        array[i] = Integer.parseInt(numbers1[i]);
    }
    System.out.print( "Array Print:  "+ Arrays.toString(array) );

答案 3 :(得分:0)

Because Scanner.nextInt() does not clear the Line Separator when the enter key is hit when the User enters a value for Array Length, it is passed to the next Scanner method which is Scanner.nextLine(). Unfortunately, because nextInt() doesn't consume the line separator it is the first thing nextLine() gets from the Scanner buffer which is basically ENTER so to speak so it skips what was typed and you end up with a NumberFormatException when trying to convert a null string with the Integer.parseInt() method.

To solve this problem use different Scanner methods or place scanner.nextLine(); directly after int input = scanner.nextInt(); so as to consume the line separator:

public static void main(String[] args) {
    Scanner scanner = new Scanner( System.in );
    System.out.print( "Enter Array Length: " );
    int input = scanner.nextInt();
    scanner.nextLine();  // Consume <-----------

    int array[] = new int[input];
    System.out.print( "Enter Array Elements: " );
    String line1 = scanner.nextLine(); // Read 1st line
    String[] numbers1 = line1.split(" "); // Split based on space
    for(int i = 0; i<numbers1.length; i++){
        array[i] = Integer.parseInt(numbers1[i]);
    }
    System.out.print( "Array Print:  "+ Arrays.toString(array) );
}

There are many ways to accomplish what you're trying to do in Java. Something you need to consider as well is, what if the User enters a alpha character like perhaps 'w' or 'o' into the elements line? Yup....NumberFormatException! You need to test for this sort of thing as well when using the methods you are using.