Results 1 to 7 of 7
  1. #1
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default How do I use try/catch inside a for loop?

    I have a code block that lets the user enter an integer value seven times:

    Java Code:
    for(i = 0; i < 7; i++){
    			
    		guess = input.readLine();
    			
    		int result = Integer.parseInt(guess;
    		userLine[i] = result;
    
    }
    Now I want to add a try/catch so that if the user tried to enter a string or a float, the loop repeats at the same iteration, instead of increasing the integer i by one.

    I've tried doing it like this:

    Java Code:
    for(i = 0; i < 7; i++){
    		try{	
    		guess = input.readLine();
    			
    		int result = Integer.parseInt(guess;
    		userLine[i] = result;
                    
                   } 
    		catch (NumberFormatException InputString)  {
    				System.out.println("Please enter an integer");
    			}
    
    }
    The catch statement executes if I enter a string, but my problem is that i increases in value anyway. I was thinking of simply adding a line "i--;" at the catch statement but I don't really think that is the correct way to do it. Any help would be greatly appreciated!
    Last edited by iwtljava; 11-27-2016 at 03:27 PM. Reason: 1

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How do I use try/catch inside a for loop?

    Use a while loop and a counter.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: How do I use try/catch inside a for loop?

    Do you mean changing my for loop to a while loop? Or putting a while loop inside the for loop?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How do I use try/catch inside a for loop?

    There is nothing wrong with decrementing the loop counter if it shouldn't increment in the for loop; it is short and efficient and it does the job ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: How do I use try/catch inside a for loop?

    I guess it's fine, my problem is that I'm not sure how to decrement is if the user input is invalid when i=0, because then i is set to -1 which gives an error. I also tried only decrementing when i>0, but that leads to the fact that the loop only executes six times instead of seven if the user input is invalid for i=0

  6. #6
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: How do I use try/catch inside a for loop?

    Nevermind, I think I solved it!

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How do I use try/catch inside a for loop?

    You can also do something like this:

    Java Code:
    for (int i= 0; i < n; ) // no increment here ...
       try {
          // do something
          i++; // increment the counter if all went well
       }
       catch (Exception e) { } // no increment here
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 8
    Last Post: 04-22-2013, 11:36 AM
  2. Catch exception, return to while loop
    By ChicagoAve in forum New To Java
    Replies: 3
    Last Post: 02-20-2011, 06:44 PM
  3. try and catch inside a for loop fails!!!
    By Kingmaha64 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 02:40 AM
  4. Try catch loop problems :'(
    By Romally in forum New To Java
    Replies: 7
    Last Post: 11-17-2010, 08:15 PM
  5. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 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
  •