0

I've been having issues catching non numbers.

I tried try / catch but I can't grasp a hold of it. If anything I get it to catch non numbers, but doesn't let the user try entering again... It just stops my code completely.

Here is my code:

package triangle;

import java.util.Scanner;

public class traingle {

public static void main(String[] args){ 
    //explaining what the program is going to do 
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");
//creating input 1,2,3 for user

Scanner angle1 = new Scanner(System.in);  // Reading from System.in
System.out.println("Enter angle degree number 1: ");
int n = angle1.nextInt();

Scanner angel2 = new Scanner(System.in);
System.out.println("Enter angle degree number 2: ");
int n2 = angel2.nextInt();

Scanner angel3 = new Scanner(System.in);
System.out.println("Enter angle degree number 3: ");
int n3 = angel3.nextInt();
//this is just telling how much degrees the user had in total if they didnt match up to triangle standards
Right leg
  • 16,080
  • 7
  • 48
  • 81
huaglo
  • 33
  • 1
  • 7
  • 3
    You only need one `Scanner` ***and*** today is a really good day to learn about *loops* **and** *arrays*. Also, why make the user enter **three** values? Prompt for two, and then ask if the third is correct. – Elliott Frisch Jan 13 '17 at 04:14
  • Consider that you do the same thing 3 times; make it a method. You also need to deal with invalid input; add a loop the ensures a valid input for each input. You need 1 array of `int`s and 1 `Scanner`; add a loop that does that for the 3 angles in the array. – ChiefTwoPencils Jan 13 '17 at 04:24
  • @ChiefTwoPencils would you be able to give me an example? sorry ive been out of java for quite some time – huaglo Jan 13 '17 at 04:27
  • Possible duplicate of [Understanding Scanner's nextLine(), next(), and nextInt() methods](http://stackoverflow.com/questions/32798803/understanding-scanners-nextline-next-and-nextint-methods) – Patrick Parker Jan 13 '17 at 04:31

2 Answers2

0

This should get you started.

package triangle;

import java.util.Scanner;

public class triangle {

    public static void main(String[] args){ 
         //explaining what the program is going to do 
        System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\n");
        System.out.println("this automated program will tell you if the angle you entered is isoceles, eqilateral or scalene");
        //creating input 1,2,3 for user

        Scanner s = new Scanner(System.in);  // Reading from System.in

        String line;
        int [] angles = new int [3];
        int count = 0;

        do {
            System.out.print("Enter angle degree number " + (count+1) + ": ");
            line = s.nextLine();
            while (true ) {
                try {
                    angles[count] = Integer.parseInt(line);
                    System.out.println("You entered " + angles[count]);
                    count++;
                    break;
                } catch (NumberFormatException e ) {
                    System.out.println("Invalid number: try again: ");
                    line = s.nextLine();
                }
            }
        } while (count < 3);


    }
}
David Choweller
  • 1,060
  • 1
  • 8
  • 7
-1

You have a simple issue.. You just use one Scanner for the code in main method.

 package triangle;

 import java.util.Scanner;

 public class traingle {

 public static void main(String[] argc){
 System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene");

 Scanner angle1 = new Scanner(System.in);  

 System.out.println("Enter angle degree number 1: ");

 int n = angle1.nextInt();

 System.out.println("Enter angle degree number 2: ");

int n2 = angel2.nextInt();

System.out.println("Enter angle degree number 3: ");

int n3 = angel3.nextInt();
minigeek
  • 2,766
  • 1
  • 25
  • 35
Shehan V
  • 164
  • 1
  • 14