2 Replies - 2910 Views - Last Post: 11 March 2010 - 11:04 PM Rate Topic: -----

#1 jason89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-March 10

how to reiterate a loop after an exception caught within a do while

Posted 11 March 2010 - 10:18 PM

Hi guys, i wish to rerun the loop should if the exception is caught, and i could'nt think of a way to do this.

Right now if the exception is caught a sys.err prints then i have an infinite loop that which i have to force kill to end the program.

heres the code fairly short one, took out just for the purpose of focusing on this matter.

Program runs fine should the input is good. Only when the exception is caught i could not think of a way to handle it.

thanks in advance

[code]

public static void main(String[] args) {
// TODO Auto-generated method stub
int hourin, maxHour=23;
boolean bool = true;
Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5
do {
try{
System.out.println("Enter hour ? : 00 - 23 " );
hourin = sc2.nextInt();

if( hourin > maxHour ){
System.out.println("Hour value not valid: 00 -23");
System.out.println("Please enter again");
}else bool = false;

}catch (NumberFormatException ex) {
System.err.println("Not a valid number: " + ex);
return;
}
catch(InputMismatchException ex){
System.err.println("Not a valid number: " + ex);
return;
}
} while(bool);
}
}

[code]

Is This A Good Question/Topic? 0
  • +

Replies To: how to reiterate a loop after an exception caught within a do while

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: how to reiterate a loop after an exception caught within a do while

Posted 11 March 2010 - 10:50 PM

View Postjason89, on 11 March 2010 - 09:18 PM, said:

Hi guys, i wish to rerun the loop should if the exception is caught, and i could'nt think of a way to do this.

Right now if the exception is caught a sys.err prints then i have an infinite loop that which i have to force kill to end the program.

heres the code fairly short one, took out just for the purpose of focusing on this matter.

Program runs fine should the input is good. Only when the exception is caught i could not think of a way to handle it.

thanks in advance


	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int hourin, maxHour=23; 
		boolean bool = true;
		Scanner sc2 = new Scanner(System.in);      // Requires J2SE 1.5
		do {
                  try{
		      System.out.println("Enter hour ? : 00 - 23 " );
		      hourin = sc2.nextInt();
		      
		      if( hourin > maxHour ){
		    	  System.out.println("Hour value not valid: 00 -23");
		    	  System.out.println("Please enter again");
	          }else bool = false;
             
             }catch (NumberFormatException ex) {
       	      System.err.println("Not a valid number: " + ex);  
       	      return;
             }
             catch(InputMismatchException ex){
    	    	 System.err.println("Not a valid number: " + ex);
    		     return;
    	     }
	} while(bool);
	}
}




here is a working solution:

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

public class s
{
	public static void main(String[] args)
	{
		int hourin, maxHour=23;
		Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5
		
		boolean bool = true;		
		boolean keepLooping; // if exception is thrown, we set this to be true.
		do 
		{
			keepLooping = false;
			try
			{
				System.out.println("Enter hour ? : 00 - 23 " );
				hourin = sc2.nextInt();

				if( hourin > maxHour )
				{
					System.out.println("Hour value not valid: 00 -23");
					System.out.println("Please enter again");
				}
				else bool = false;

			}
			catch (NumberFormatException ex) 
			{
				System.out.println("Not a valid number: "); 
				keepLooping = true; // we need to loop again
				sc2.next(); // eat the '\n'
			}
			catch(InputMismatchException ex)
			{
				System.out.println("Not a valid number: ");
				keepLooping = true; // we need to loop again
				sc2.next();// eat the '\n'
			}
		} while(bool || keepLooping);
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 jason89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-March 10

Re: how to reiterate a loop after an exception caught within a do while

Posted 11 March 2010 - 11:04 PM

Aha, brilliant stuff thanks alot.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1