Results 1 to 8 of 8
Thread: Try catch loop problems :'(
- 11-17-2010, 06:06 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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 ClassLast edited by Romally; 11-17-2010 at 06:21 PM.
- 11-17-2010, 06:40 PM #2
...should be:Java Code:while (i = false)
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.
- 11-17-2010, 06:58 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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.
- 11-17-2010, 07:12 PM #4
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.)Java Code:while (i == false) { //Begin while
- 11-17-2010, 07:27 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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
- 11-17-2010, 07:33 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-17-2010, 07:56 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 12
- Rep Power
- 0
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!
- 11-17-2010, 08:15 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Loop problems
By jim01 in forum New To JavaReplies: 3Last Post: 10-18-2010, 12:49 AM -
try-catch InputMismatchException in a while loop
By themulator in forum New To JavaReplies: 17Last Post: 10-12-2010, 04:49 AM -
Try / Catch Problems
By fullmetaljacket in forum New To JavaReplies: 1Last Post: 06-28-2009, 09:18 PM -
Newbie having problems with for loop
By Dannii in forum New To JavaReplies: 4Last Post: 04-13-2009, 11:52 PM -
Problems with while loop
By Albert in forum New To JavaReplies: 2Last Post: 07-04-2007, 07:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks