Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Try... catch loop?!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Try... catch loop?!

    I need to write code to see if three numbers make a right angled triangle (which I have done below). However, I need to use a try/catch as well as keep prompting the user to enter a number only instead of anything else... (a while loop would be best?) not sure how to go about this though..

    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
     
    public class TriangleTest
    {
        public static void main (String[] args) throws Exception 
        {
            Scanner s = new Scanner(System.in);  
     
                try
                {        
                    System.out.println("Enter side 1: ");
                        int side1 = s.nextInt();              
                    System.out.println("Enter side 2: ");
                        int side2 = s.nextInt();
                    System.out.println("Enter side 3: ");
                        int side3 = s.nextInt(); 
                    if(((side1*side1) == ((side2*side2) + (side3*side3))) ||
                    ((side2*side2) == ((side1*side1) + (side3*side3))) ||
                    ((side3*side3) == ((side1*side1) + (side2*side2))))
                        System.out.println("True! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " make a triangle.");
                    else
                        System.out.println("False! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " don't make a triangle.");
                }catch(InputMismatchException exception)
                    {
                        System.out.println("Please enter numbers only!");   
                        System.out.println("");
                }                
     
            s.close();
        }         
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Try... catch loop?!

    You could use a while loop. Considering your specs, I assume you want to keep prompting, even if the user enters something other than a number? If so, you would want to put the entire try/catch block inside the while loop.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    11
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Try... catch loop?!

    The logic is somewhat below, although... after the completion of entering three numbers (which I haven't done yet) OR wrong input I want to ask the user to try again and keep prompting as well as giving them the option to exit. Below it displays the message if input is wrong, although goes straight back to the start of the loop and skips the option to continue or not. If this is outside the loop, it infinite loops.

    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
     
    public class TriangleTest
    {
        public static void main (String[] args) throws Exception 
        {
            Scanner s = new Scanner(System.in);  
            String response = "yes";
            while(!response.equals("no"))
            {
                try
                {        
                    System.out.println("Enter side 1: ");
                        int side1 = s.nextInt();              
                    System.out.println("Enter side 2: ");
                        int side2 = s.nextInt();
                    System.out.println("Enter side 3: ");
                        int side3 = s.nextInt(); 
                    if(((side1*side1) == ((side2*side2) + (side3*side3))) ||
                    ((side2*side2) == ((side1*side1) + (side3*side3))) ||
                    ((side3*side3) == ((side1*side1) + (side2*side2))))
                        System.out.println("True! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " make a triangle.");
                    else
                        System.out.println("False! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " don't make a triangle.");
                }catch(InputMismatchException exception)
                {
                    System.out.println("Please enter numbers only!");   
                    System.out.println("");
                    System.out.println("Continue? 'yes' or 'no' to exit!" );
                    reponse = s.nextLine();
                }
            }
            s.close();
        }        
    }

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Try... catch loop?!

    It was somehow reading in the \n character. This should fix it.

     
     
    import java.util.Scanner;
    import java.io.*;
    import java.util.*;
     
    public class TriangleTest
    {
       public static void main (String[] args) throws Exception 
       {
          Scanner s = new Scanner(System.in);  
          String response = "yes";
          while(!response.equals("no"))
          {
             try
             {        
                System.out.println("Enter side 1: ");
                int side1 = s.nextInt();              
                System.out.println("Enter side 2: ");
                int side2 = s.nextInt();
                System.out.println("Enter side 3: ");
                int side3 = s.nextInt(); 
                if(((side1*side1) == ((side2*side2) + (side3*side3))) ||
                    ((side2*side2) == ((side1*side1) + (side3*side3))) ||
                    ((side3*side3) == ((side1*side1) + (side2*side2))))
                   System.out.println("True! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " make a triangle.");
                else
                   System.out.println("False! These numbers " + side1 + ", " 
                                        + side2 + " and " + side3 + " don't make a triangle.");
             }
             catch(InputMismatchException exception)
             {
     
                System.out.println("Please enter numbers only!");   
                System.out.println("");
                System.out.println("Continue? 'yes' or 'no' to exit!" );
                s.nextLine(); // consumes linebreak character
                response = s.nextLine();
     
             }
          }
          s.close();
       }        
    }


    I thought it wasn't clearing the buffer or something so I looked it up on Google and found this: java - Scanner issue when using nextLine after nextXXX - Stack Overflow

  5. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    deeevo (October 11th, 2013)

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Try... catch loop?!

    There is no such thing as a try catch loop!
    Improving the world one idiot at a time!

Similar Threads

  1. Try and catch
    By jaydac12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 10th, 2013, 05:58 AM
  2. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 11:14 AM
  3. [SOLVED] While try catch loop
    By Duaber in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 02:14 PM
  4. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  5. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 03:20 PM

Tags for this Thread