1

How do I make it loop back to the start of the FOR loop again? I had try searching the answers. Use of continue don't seems to work for me.

try{

    for (int i = 0; i < Array1.length; i++){
        System.out.println("enter Values ");

        Scanner input = new Scanner(System.in);
        Array1[i]= input.nextInt();
    }
}//try
catch (InputMismatchException e) 
{
    System.out.println("pls enter integers only ");

}//catch

How do I go about continuing the process again? For example

Enter Values 5 enter Values 1 enter Values g pls enter integers only ->> error exception is over here

After this error show, how am I able to continue the enter values process without retyping from value 1?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Gerald Tan
  • 21
  • 1
  • 4

6 Answers6

3

You'll have to put the try/catch inside the for loop (and decrease i in the catch block, so all the indexes will be filled).

Since people seem to be confused about the basics here, why not have the whole fricking code:

Scanner input = new Scanner(System.in);
System.out.println("Enter integer values: ");
for (int i = 0; i < Array1.length; i++){
    try {
        Array1[i]= input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("Please enter integers only");
        i--;
    }
}
// Now your Array1 is filled with ints
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2
    Absolutely redundant, `try- catch` inside for loop, No ? – Suresh Atta Oct 18 '13 at 05:50
  • 1
    Nothing redundant about it. That's where the exception is thrown (from nextInt()). If you decrease i in the catch block, the for loop won't terminate (and the index won't increase) until you've inserted valid integers. – Kayaman Oct 18 '13 at 05:53
  • @sᴜʀᴇsʜᴀᴛᴛᴀ it is not. – Luiggi Mendoza Oct 18 '13 at 05:53
  • Maybe leave a comment why it is a damn good idea to not initialize a Scanner inside the for loop, but put initialization before (as you have correctly done). Good thing to educate the OP a bit more. BTW +1 from me. – Matthias Oct 18 '13 at 06:01
1

You say how to roll back if an exception occours? Can't you insert the try/catch statement inside the for cycle?

Scanner input = null;

for (int i = 0; i < Array1.length; i++){
    System.out.println("enter Values ");

    input = new Scanner(System.in);
    try {
        Array1[i]= input.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("pls enter integers only ");
    }
}

CODE EDITED: best programming use is to declare variables outside loops

Lorenzo Barbagli
  • 1,241
  • 2
  • 16
  • 34
1

it will start afresh if any exception occurs.

    for(int i=0;i<a.length;i++){
        try{
            System.out.println("enter Values ");
            Scanner input = new Scanner(System.in);
            a[i]=input.nextInt();
        }
        catch(InputMismatchException e){
            System.out.println(e.getMessage());
            i=-1;
        }
    }

below will just try to fetch the value where it fired exception:

for(int i=0;i<a.length;i++){
            try{
                System.out.println("enter Values ");
                Scanner input = new Scanner(System.in);
                a[i]=input.nextInt();
            }
            catch(InputMismatchException e){
                System.out.println(e.getMessage());
                i--;
            }
        }
Trying
  • 14,004
  • 9
  • 70
  • 110
1

Try using the following code.

try{
            for (int i = 0; i < Array1.length; i++){
                System.out.println("enter Values ");

                Scanner input = new Scanner(System.in);
                Array1[i]= input.nextInt();
                if(i==Array1.length-1) i=0;
            }
        }//try
        catch (InputMismatchException e) 
        {
            System.out.println("pls enter integers only ");

        }

which will consistently execute the same loop. Is this what you want?

pnathan
  • 713
  • 3
  • 9
  • what i want is to loop back to tell user to enter an integer again.without forgetting the values the user had type before the error occur – Gerald Tan Oct 18 '13 at 06:18
0

If you want to start the entire process again upon catching the exception,

boolean isValid = false;

while(isValid == false) {
try{

    for (int i = 0; i < Array1.length; i++){
        System.out.println("enter Values ");
        Scanner input = new Scanner(System.in);
        Array1[i]= input.nextInt();
    }
    isValid = true;
}//try
catch (InputMismatchException e) 
{
    System.out.println("pls enter integers only ");

}//catch
}
Se Won Jang
  • 773
  • 1
  • 5
  • 12
0
try{
for (int i = 0; i < Array1.length; i++){    
System.out.println("enter Values ");
Scanner input = new Scanner(System.in);    
    Array1[i]= input.nextInt();
}
} 
catch (InputMismatchException e) {System.out.println("pls enter integers only ");
i = 0;
continue;
}

Hope it helps.

Ashish
  • 735
  • 1
  • 6
  • 15