Search code examples
arraysfor-loopwhile-loopuser-inputinputmismatchexception

InputMismatchException issue with For loops and If Else Statement


I'm new to Java. I would really appreciate it if you could help me figure this out. I'm trying to make a program to read user inputs (integers) and store them into an array and print them out afterwards. I use a variable called currentSize to keep track of how many variables were inserted.

Since I dont know how many inputs there are going to be, every time the element numbers equals the array length, I use Arrays.copyOf method to double the size of the existing array.

I use a while loop with in.hasNextInt() with the goal to exit the while loop once the user enters something else such as a letter rather than an integer.

My problem is it keeps throwing InputMismatchException although the idea is for it to exit the while loop once a non-integer value is entered.

As I was trying to pinpoint where it went wrong I added 2 print statements to make sure the number of elements is counting correctly and that Array length is increasing its size.

System.out.println("No of elements: " + currentSize);
System.out.println("Array size: " + numList.length);

I have tried another approach and got it to work the way I wanted without the for loop so I suspected the while loop is the issue.

import java.util.Scanner;
import java.util.Arrays;

public class ArrayPrinter{
    public static int DEFAULT_LENGTH = 2;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        //keep track of how many element we insert
        int currentSize = 0;
        int[] numList = new int[DEFAULT_LENGTH];

        System.out.println("Please insert value to store in array: ");
        while(in.hasNextInt()){
            for(int i = 0; i < numList.length; i++){
                numList[i] = in.nextInt();
                currentSize++;
                System.out.println("No of elements: " + currentSize);
                System.out.println("Array size: " + numList.length);
                if(currentSize == numList.length){
                    numList = Arrays.copyOf(numList, currentSize * 2);
                }       
            }
        }
        for(int number : numList){
            System.out.print(number + " ");
        }
    }
}

It may be just something very easy but I have looked through all the other posts on Stack but to no avail.

Thank you so much!


Solution

  • There is a problem with your algorithm. The line containing: while(in.hasNextInt()) will run only one time, before the first input. After that your second loop for(int i = 0; i < numList.length; i++) will run indefinitely or until you type an invalid integer.

    In order to understand the problem you need to look exactelly at the line where the exception happens: numList[i] = in.nextInt();. The method in.nextInt is not able to handle an invalid input.

    You need only the "for" loop and you need to use hasNextInt inside it.

    for (int i = 0; i < numList.length; i++) {
        if (in.hasNextInt()) {
            numList[i] = in.nextInt();
            currentSize++;
            System.out.println("No of elements: " + currentSize);
            System.out.println("Array size: " + numList.length);
            if (currentSize == numList.length) {
                numList = Arrays.copyOf(numList, currentSize * 2);
            }
        }
    }
    
    for (int number : numList) {
        System.out.print(number + " ");
    }
    

    I understand that you are playing around with loops and arrays in order to learn it. However, to implement this logic in a simpler way you should use a List. A List (i.e.: ArrayList) can handle automatically a variable number of items and your final code would be much simpler.