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

1. ## 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("");
}

s.close();
}
}```

2. ## 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.

3. ## 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("");
System.out.println("Continue? 'yes' or 'no' to exit!" );
reponse = s.nextLine();
}
}
s.close();
}
}```

4. ## 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("");
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. ## Re: Try... catch loop?!

There is no such thing as a try catch loop!