Results 1 to 8 of 8
  1. #1
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Try catch loop problems :'(

    I'm trying to get this code segment to repeat while the exception is caught. I don't see any errors in the code, neither does the compiler, except for the local boolean declared to set the "i" to true, it says it is never read. The program terminates before it is able to execute the account method.


    Java Code:
    //Import
    import java.util.Scanner;
    import java.util.InputMismatchException;
    //End import
    // ----------------------------------------------------------------------
    //Begin WelcomeScreen Class
    public class WelcomeScreen
    {
    	Scanner input = new Scanner(System.in); //Create scanner object
    	DataStorage data = new DataStorage(); //Create DataStorage object for use
    	
    //Begin welcome	
    	
    	public void welcome()//Declare method
    	{
    		
    		System.out.print("Welcome. Please enter your name\n\n"); //Display welcome message
    		String userName = input.next(); //Wait for input
    		data.setname(userName); //Value is set through setname's parameters
    		/*
    		 * Removed for redundancy. See method "account"
    		 * System.out.print("\nHello " + userName); //Display text + input
    		 */
    		
    	}//End welcome
    	
    //Begin account	method
    	
    	public void account()//Declare method
    		
    	{
    		boolean i = true;//Local variable declare for loop segment
    		
    	try//Try segment containing loop
    	{
    		
    		
    		
    		while (i = false)
    		
    		{
    				System.out.printf("\n\n%s please enter your account number", data.getname());
    				int account = input.nextInt();//Input = int
    				data.setaccount(account);//Store input in DatStorage class
    				
    		}//End while loop
    		
    			
    	} catch (InputMismatchException e)//Catch exception when input ! int
    	
    	{
    		
    	/*
    	 * System.out.print(e);//Print exception for error checking
    	 */
    		
    	i = false;//Variable checked by while statement to repeat loop after exception catch
    	
    	}//End Catch
    	
    	}//End account method
    	
    	
    	
    	
    	
    	
    	
    	
    }//End WelcomeScreen Class
    Last edited by Romally; 11-17-2010 at 06:21 PM.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Java Code:
    while (i = false)
    ...should be:
    Java Code:
    while (i == false)

    EDIT:
    Actually there's more to it than that... The while loop should be AROUND the try-catch statement, and i should be set to **true** when you want the loop to stop.

    Basically this is the idea:
    Java Code:
    set your variable to false
    while (your variable is false) {
        try {
            something
            break loop by setting variable to true
        } catch (exception) {
            tell user
        }
    }
    Last edited by Zack; 11-17-2010 at 06:43 PM.

  3. #3
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Alright, I followed your instructions, however I still could not repeat the while statement if the user threw an exception. Here is the code:

    Java Code:
    public void account()//Declare method
    		
    	{//Begin method
    		boolean i = false;
    		
    		while (i = false);
    		{//Begin while
    			try
    			{//Begin try
    				
    				System.out.printf("\n\n%s please enter your account number: ", data.getname());
    				
    				int account = input.nextInt();//Input = int
    				
    				data.setaccount(account);//Store input in DatStorage class
    				
    				i = true;			
    				
    			} catch (InputMismatchException e) //End Try
    			
    			{//Begin catch
    				
    				System.out.println(e + "\n\nTry again.");
    				i = false;
    			}//End catch
    			
    			
    			
    			
    		}//End while
    	}//End method

    It terminates after printing out the exception plus the text. i = false; is never evaluated by the while to repeat the loop.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Java Code:
    		while (i = false);
    		{//Begin while
    This does not begin a while statement. Basically, these two lines translate to this:
    Java Code:
    while (i = false) { }
    //Begin while
    Therefore you should be doing this instead:
    Java Code:
    while (i == false)
    { //Begin while
    Additionally, notice the double equals sign in my code above. = is used to assign a value to a variable (such as when you declare i on line 4), whereas == is used to compare two values. (More info here.)

  5. #5
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    Thank you for the link.

    Now, the code DOES work, however, it spirals into an infinite loop once the exception is thrown. I tried modifying the code to break once the exception is thrown, but that just terminates the program all together. I tried i = false; and then break, hoping the while statement would reread the value and compare, it didn't. How do I prevent the the infinite loop after the exception gets caught?


    Java Code:
    public void account()//Declare method
    		
    	{//Begin method
    		boolean i = false;
    		
    		while (i == false)
    		{//Begin while
    			try
    			{//Begin try
    				
    				System.out.printf("\n\n%s please enter your account number: ", data.getname());
    				
    				int account = input.nextInt();//Input = int
    				
    				data.setaccount(account);//Store input in DatStorage class
    				
    				i = true;
    				
    			} catch (InputMismatchException e) //End Try
    			
    			{//Begin catch
    				
    				System.out.println(e + "\n\nTry again.");
    				
    			}//End catch
    			
    			
    			
    			
    		}//End while
    	}//End method

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Romally View Post
    Thank you for the link.

    Now, the code DOES work, however, it spirals into an infinite loop once the exception is thrown. I tried modifying the code to break once the exception is thrown, but that just terminates the program all together. I tried i = false; and then break, hoping the while statement would reread the value and compare, it didn't. How do I prevent the the infinite loop after the exception gets caught?


    Java Code:
    public void account()//Declare method
    		
    	{//Begin method
    		boolean i = false;
    		
    		while (i == false)
    		{//Begin while
    			try
    			{//Begin try
    				
    				System.out.printf("\n\n%s please enter your account number: ", data.getname());
    				
    				int account = input.nextInt();//Input = int
    				
    				data.setaccount(account);//Store input in DatStorage class
    				
    				i = true;
    				
    			} catch (InputMismatchException e) //End Try
    			
    			{//Begin catch
    				
    				System.out.println(e + "\n\nTry again.");
    				
    			}//End catch
    			
    			
    			
    			
    		}//End while
    	}//End method
    When the nextInt() method throws an Exception (because it can't read an int) it doesn't read anything at all; when you attempt to read an int using that same method, the same will be checked and the same Exception will be thrown again. A solution would be, when an Exception is thrown, to read and get rid of the entire offending line using the nextLine() method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    MWUAHAHAHA I GOT IT!!! WOOOOT!

    I had to change all the methods to string and parse int to be stored if true. It was the only way it would let me use nextLine();

    Java Code:
    public void account()//Declare method
    		
    	{//Begin method
    		boolean i = false;
    		
    		while (i == false)
    		{//Begin while
    			try
    			{//Begin try
    				
    				System.out.printf("\n\n%s please enter your account number: ", data.getname());
    				
    				String account = input.nextLine();//Input = int
    				Integer.parseInt(account);
    				
    				data.setaccount(account);//Store input in DatStorage class
    				
    				i = true;
    				
    			} catch (NumberFormatException e) //End Try
    			
    			{//Begin catch
    				
    				System.out.println(e + "\n\nTry again.\n\n");
    	
    			}//End catch

    Mwuahahaha thats so awesome. I've been thinking about this and trying anything I could think of for TWO days! Man this feels awesome. Thanks for the help!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by Romally View Post
    MWUAHAHAHA I GOT IT!!! WOOOOT!
    Nurse, syringe please ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Loop problems
    By jim01 in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:49 AM
  2. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM
  3. Try / Catch Problems
    By fullmetaljacket in forum New To Java
    Replies: 1
    Last Post: 06-28-2009, 09:18 PM
  4. Newbie having problems with for loop
    By Dannii in forum New To Java
    Replies: 4
    Last Post: 04-13-2009, 11:52 PM
  5. Problems with while loop
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 07:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •