0
import java.util.InputMismatchException;
import java.util.Scanner;

public class array
{
    public static void main(String[] args)
    {
        int marks=0;
        Scanner input = new Scanner(System.in);
        int array[] = new int[5];
        System.out.println("Please enter the marks of students one by one");
        for(int i = 0;i < array.length;i++)
        {
            try
            {
                marks = input.nextInt();

            }
            catch(InputMismatchException e)
            {
                System.out.println(e);
                System.out.println("Please enter an integer value");
            }
            array[i]=marks;

        }
        System.out.println("index     Value" );
        for(int i=0;i<array.length;i++)
        {
            System.out.println("    "+i+"        "+array[i]);
        }
    }
}

Above is my code. I tried to catch the exception of the user typing any other data other than integer value and its working fine as it is catching the exception. But the problem is that it is not giving control back to the user. You will understand it better with the output. Following is message i am getting while execution. It is text copied from command prompt.

Please enter the marks of students one by one
abc
java.util.InputMismatchException
Please enter an integer value
java.util.InputMismatchException
Please enter an integer value
java.util.InputMismatchException
Please enter an integer value
java.util.InputMismatchException
Please enter an integer value
java.util.InputMismatchException
Please enter an integer value
index     Value
    0        0
    1        0
    2        0
    3        0
    4        0

As you could see as i entered 'abc' as input it catched the exception and gave out the proper message but it did it for 5 times i.e. size of my array.

Why is it not giving the control back to user?

Wyzard
  • 31,764
  • 3
  • 60
  • 78
  • Please give us compilable code. This will not compile due to mismatched `try` braces, and because the `try` is inside the `for` and the `catch` is not. – Jashaszun Jul 10 '14 at 22:47

3 Answers3

1

The problem is that when exception happens, you still count that attempt and then storing the value. You need to do something like this:

for(int i = 0;i < array.length;i++) {

  boolean validInput = false;
  do {
     try {
        marks = input.nextInt();
        array[i]=marks;
        validInput = true;
     } catch (InputMismatchException e) {
        System.out.println("Please enter an integer value");
     }
  } while (!validInput);

}

In this code the user would have to input the proper value in order to continue (internal do while loop)

dimoniy
  • 5,120
  • 2
  • 19
  • 21
0

The Scanner class will not pass up the invalid input until you handle it. This is to give the programmer another chance to do something with the data, rather than just immediately throwing it away. Please try calling input.nextLine(); either in the catch statement, or just after it? This should discard the remaining input.

As such, the loop would then look like:

    for(int i = 0;i < array.length;i++)
    {
        try
        {
            marks = input.nextInt();
        }
        catch(InputMismatchException e)
        {
            System.out.println(e);
            System.out.println("Please enter an integer value");
            input.nextLine();
        }
        array[i]=marks;
    }

See Using scanner.nextLine() for more potential answers?

Community
  • 1
  • 1
Serge
  • 1,814
  • 6
  • 20
  • 32
  • I'm suggesting to potentially use it after the catch statement, in case the program starts acting strangely when passing valid data, due to a possible newline character or whitespace following the valid integer. – Serge Jul 10 '14 at 23:54
0

when you init Scanner () object in the instance variable "input", although you set it to "0" in the exception, but he still continues persisting remnants of object initialized in the instance variable. A quick solution is to initialize a new Scanner object again every time the loop is run. Whereupon the code:

package Teste;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Array
{
public static void main(String[] args)
{
    int marks=0;
    Scanner input = null;
    int array[] = new int[5];
    System.out.println("Please enter the marks of students one by one");
    for(int i= 0;i < array.length;i++)
    {
        try
        {
            input = new Scanner(System.in);
            marks = input.nextInt();
        }
        catch(InputMismatchException e)
        {
            System.out.println(e);
            System.out.println("Please enter an integer value");
        }
        array[i]=marks;
    }
    System.out.println("index     Value" );
    for(int i=0;i<array.length;i++)
    {
        System.out.println("    "+i+"        "+array[i]);
    }
}
}

where output

Please enter the marks of students one by one
a
java.util.InputMismatchException

Please enter an integer value

2

3

4

5

index     Value
0        0
1        2
2        3
3        4
4        5