2
import java.util.Scanner;

public class DRYeck {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);                               //declaration scanner

        System.out.println("What is given:");

        System.out.print("How many sides?");                                    //input the number of Sides that are given
        System.out.println("");
        double input = scanner.nextInt();
        System.out.print("How many angles? ");                                  //input the number of Angles that are given 
        System.out.println("");
        double input2 = scanner.nextInt();

        if((input+input2)>3){                                                   //observes if the input is higher than 3 and errors if this is so

            System.out.println("Please enter only 3 entries, you entered "+((int)(input+input2))+" eingegeben...");

        }else if((input==1) && (input2==2)){

            double side1 = inputDialogSide();                                   //input a Side
            double angle1 = inputDialogAngle();                                 //input an Angle
            double angle2 = inputDialogAngle();                                 //input an Angle

            oneSideTwoAnglesGeneral(side1,angle1,angle2);

        }else if((input==2) && (input2==1)){

            System.out.println("Is the angle inclsive or exclusiv?");

            String input3 = scanner.nextLine();                                 //input 3 for identification if the angle is inclusive or exclusive

            if(input3.equals("inklusiv")){                                      //set variable 'inclu' to true if 'inclusive' is entered
                                                                                //distinction of cases into inclusive and exclusive angle

                double side1 =  inputDialogSide();                              //input one Side
                System.out.println("");
                double side2 = inputDialogSide();                               //input the other Side
                System.out.println("");
                double angle1 = inputDialogAngle();                             //input the Angle

                twoSidesOneAngleIncl(side1,side2,angle1);

            }else{

                double side1 =  inputDialogSide();                              //input one Side
                double side2 = inputDialogSide();                               //input the other Side
                double angle1 = inputDialogAngle();                             //input the Angle

                twoSidesOneAngleExcl(side1,side2,angle1);

            }
        }else if((input==3) && (input2==0)){                                                        //condition if 3 Sides are inputed

            double side1 = inputDialogSide();                                   //input first Side
            double side2 = inputDialogSide();                                   //input second Side
            double side3 = inputDialogSide();                                   //input third Side

            threeSides(side1,side2,side3);

        }

    }

    public static double inputDialogAngle(){                                    //subroutine for inputing an Angle
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an angle: ");
        double input5 = scanner.nextDouble();                                   //input5(variable) for an Angle
        System.out.println("");
        return input5;
    }

    public static double inputDialogSide(){                                     //subroutine for inputing a Side
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the sidelength: ");
        double input4 = scanner.nextDouble();                                   //input4(variable) for a Side
        System.out.println("");
        return input4;
    }


    static void collection(double alpha,double beta, double gamma,double sidea,double sideb,double sidec){  //sorting the value in the array

        double[][] SidesAngles = new double [3][2];                             //array for side length value of the angles

        for(int i=0;i<SidesAngles.length;i++){
            for(int j=0;j<SidesAngles[i].length;j++){
                if((SidesAngles[0][0]==0)){
                    SidesAngles[0][0]=sidea;
                }else if((SidesAngles[0][1]==0)){
                    SidesAngles[0][1]=alpha;
                }else if((SidesAngles[1][0]==0)){
                    SidesAngles[1][0]=sideb;
                }else if((SidesAngles[1][1]==0)){
                    SidesAngles[1][1]=beta;
                }else if((SidesAngles[2][0]==0)){
                    SidesAngles[2][0]=sidec;
                }else if((SidesAngles[2][1]==0)){
                    SidesAngles[2][1]=gamma;
                }
            }
        }
        PrintArray(SidesAngles);
    }

    public static double angleDiff(double angle1, double angle2){
        double resuAng = 180-angle1-angle2;
        return resuAng;
    }

    static void PrintArray(double SidesAngles[][]){ //printing the Array with all values
        String[][] AngleSideNames = {{"Seite a","Alpha"},{"Seite b","Beta"},{"Seite c","Gamma"}};   //Names of angles and Sides

        for(int i=0;i<SidesAngles.length;i++){
            for(int j=0;j<SidesAngles[i].length;j++){
                System.out.print(AngleSideNames[i][j]+": "+SidesAngles[i][j]+"\n");
            }
        }
    }

    public static double sinusRule(double angle1, double angle2, double side){
        double angle3 = angleDiff(angle1,angle2);                                           
        double resuSide = side*(Math.sin(angle1)/Math.sin(angle2));
        return resuSide;
    }

    static void threeSides(double sidea, double sideb, double sidec){           //subroutine for 3 Sides that are given
        double alpha = Math.acos((Math.pow(sidea, 2)+Math.pow(sideb, 2)-Math.pow(sidec, 2))/(2*sidea*sideb));
        alpha = Math.toDegrees(alpha);                                          //alpha in degree
        double beta = Math.acos((Math.pow(sidea,2)+Math.pow(sidec,2)-Math.pow(sideb,2))/(2*sidea*sidec));
        beta = Math.toDegrees(beta);                                            //output in degree
        double gamma = angleDiff(beta,alpha);                                   //determine the angle differance
        collection(alpha,beta,gamma,sidea,sideb,sidec);

    }

    static void twoSidesOneAngleIncl(double sidea, double sideb, double gamma){ //subroutine for 1 Angle(inclusive) and 2 Sides
        double sidec = Math.sqrt(Math.pow(sidea,2)+Math.pow(sideb,2)-(2*sidea*sideb*Math.cos(gamma)));
        double alpha = Math.acos((Math.pow(sideb,2)+Math.pow(sidec, 2)-Math.pow(sidea, 2))/(2*sideb*sidec));
        double beta = angleDiff(alpha,gamma);
        collection(alpha,beta,gamma,sidea,sideb,sidec);
    }

    static void twoSidesOneAngleExcl(double sideb, double sidec, double beta){  //subroutine for 1 Angle(exclusive) and 2 Sides 

        double sidea=0,alpha=0,gamma=0;                                         //initialize sidea, alpha, gamma
        double d = ((sidec/sideb)*Math.sin(beta));                              //variable for further movement
        boolean acute=false,obtuse=false;                                       //initialize check-variable for acute or obtuse triangle

        if(d>1){                                                                //condition if check-variable is higher than 1
            System.out.println("There is no solution for this triangle");
        }else if(d==1){                                                         //if check-variable equals 1
            gamma = 90;                                                         //gamma=90° because of the conditions
            sidea = Math.sqrt(Math.pow(sideb,2)+Math.pow(sidec,2));             //solve sidea with pythagorean theorem
        }else{                                                                  //if check-variable is lower than 
            System.out.print("Is the traingle obtuse or actuse: ");
            Scanner scanner = new Scanner(System.in);                           //input for acute or obtuse statement
            String input = scanner.nextLine();

            if(input.equals("obtuse")){                                         //sets the right boolean to true so further operations are correct
                acute = true;
            }else if(input.equals("acute")){
                obtuse = true;
            }

            if((sideb<sidec) && (acute)){                                       //checks if b<c and the triangle should be acute
                gamma = Math.asin(d);
                System.out.println("angle gamma: "+gamma);

                sidea = sinusRule(beta,gamma,sideb);                            //Side a

            }else if((sideb<sidec) && (obtuse)){                                //checks if b<c and the triangle should be obtuse
                gamma = Math.asin(d);
                gamma = 180-gamma;
                System.out.println("angle gamma: "+gamma);

                sidea = sinusRule(beta,gamma,sideb);                            //Side a

            }
            System.out.println("There is no solution!!!");
        }
        collection(alpha,beta,gamma,sidea,sideb,sidec);
    }

    static void oneSideTwoAnglesGeneral(double sidec, double alpha, double beta){//subroutine for one side and two angles that are given
        double gamma = angleDiff(alpha,beta);                                   //determine the third angle
        double sidea = sidec*(Math.sin(alpha)/Math.sin(gamma));                 //determine side with the law of sinus
        sinusRule(alpha,gamma,sidec);
        double sideb = sidec*(Math.sin(beta)/Math.sin(gamma));                  //same here to determine side b
        sinusRule(beta,gamma,sidec);

        collection(alpha,beta,gamma,sidea,sideb,sidec);
    }
}

Exception:

Exception in thread "main" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Unknown Source)
   at java.util.Scanner.next(Unknown Source)
   at java.util.Scanner.nextDouble(Unknown Source)
   at DRYeck.inputDialogSide(DRYeck.java:85)
   at DRYeck.main(DRYeck.java:52)`

My Question is now how to get rid of the exception. This Program is meant to calculate all sides and angles of an triangle with only 3 inputs. For example one angle and 2 sides or 2 angles and 1 side and so on.

blueblade
  • 23
  • 4
  • change `double input = scanner.nextInt(); ` to double input = `scanner.nextDouble();` – Kenneth Clark Jun 06 '15 at 08:16
  • 1
    What is your input for this line? `double input = scanner.nextInt();` – Magnilex Jun 06 '15 at 08:17
  • I changed double input = scanner.nextInt(); into `ìnt input = scanner.nextInt();` So typing the number of the sides or angles that are given. So that the inputed number from the scanner (input) and (input2) is not allowed to be higher than 3 integer should work perfectly fine as it does. – blueblade Jun 06 '15 at 09:21
  • Your question is a bit strange to me. Why do you enter something like `"inklusiv"` if the program asks for *"Enter the sidelength"*. Haven't you noticed, that the *"Is the angle inklsiv or exclusiv?"* question was skipped? – Tom Jun 06 '15 at 10:23
  • possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Jun 06 '15 at 10:26
  • I can't follow your argumentation at all... The program structure is that: 1)first of all th user is asked to enter how many sides and angles are given from the triangle that you want to draw or something. 2)Than according to you input there are some possibilities to solve the other values that you need. One of them is if you enter 2 sides and 1 angle to ask if the angle is between the 2 sides(inclusive) or is one of the other angles and they exclusive (I defined it like that) Thats why it has to ask that questions, sorry for that much info but i cant see what is skipped exactly. – blueblade Jun 06 '15 at 10:41
  • So I studied the link you merged here (not sure if I said that right) and my problem would be solved if I `nextLine();` after the Scanner input line and though I tried that bevor and that didn't solve the problem I had... – blueblade Jun 06 '15 at 10:53
  • @blueblade Run your code without the fix of the answer and enter a number for amount of sides (it should be *"How many sides"* btw (just an info :) )) and then the amount of angles (also *many* instead of *much* :) ) and then you will see, that you program asks about *inklsiv or exclusiv* and directly after that about the *sidelength*. It skipps the *inklsiv or exclusiv* question and doesn't let you enter something for that. If you want to know why: read the linked duplicate question :). – Tom Jun 06 '15 at 10:54
  • You need to add `nextLine` right after a `nextInt` call to read the remaining *line delimiter* from the input stream. You could also do what @alainlompo suggested, because this is also correct, but the explanation why it is correct, isn't quite right. – Tom Jun 06 '15 at 10:56
  • Ok now I got the idea and what you meant... I will edit 'much' into 'many' (sorry btw english is not my first language ^^)... Well, although thank you for your hint anyways :) – blueblade Jun 06 '15 at 11:07
  • Well, the important part to learn here is: getting input using `Scanner` can be tricky. Also mind that `next()` only reads a single token. So for example, if you ask the user to enter his age (using `nextInt()`) and then for his full name, you can't read input like *"John Doe"* using `next()`, because it will only return *"John"*. You can call `next()` twice, but this can't handle names like *"John William Doe"*. So `nextLine()` would be the correct choice (with the applied fix of the duplicate question). – Tom Jun 06 '15 at 11:15

2 Answers2

1

Your issue is related to this section of your code:

else if((input==2) && (input2==1)){

            System.out.println("Is the angle inklsiv or exclusiv?");

            String input3 = scanner.nextLine();                                 //input 3 for identification if the angle is inclusive or exclusive

            if(input3.equals("inklusiv")){ 

Since you are not requesting a full line here but just a token, use Scanner.next() instead of Scanner.nextLine();

public String next() Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

So change this line:

 String input3 = scanner.nextLine();

to

 String input3 = scanner.next();

And your program should work fine!

alainlompo
  • 4,414
  • 4
  • 32
  • 41
  • Thank so much. :) I wasn't abel to solve this problem for 2 days now and it got me almost crazy. And it work very fine. ^^ – blueblade Jun 06 '15 at 08:49
  • Sorry to say that, but your explanation about the problem is not correct. The change fixes his problem, but for another reason? Can you explain, why you think that OP shouldn't use `nextLine` if he just wants a single token? Using `nextLine` is fine, if you're aware of the other problem(s). – Tom Jun 06 '15 at 10:25
  • What do you mean by "if you're aware of the other problems(s)."? – blueblade Jun 06 '15 at 10:43
  • @blueblade The "real" problem is, that you're not reading the remaing line delimiter from the input stream. That is what I meant if "other problem" and since you're calling `nextInt` several times, I used the plural. I don't mean that there are issues in your code, except the one with the user input. Sorry if that wasn't clear. – Tom Jun 06 '15 at 10:59
0

I think there is a problem here:

double input = scanner.nextInt();

change it to this and all other occurences:

double input = scanner.nextDouble();
Identity1
  • 1,139
  • 16
  • 33