Search code examples
javaloopsif-statementmethodsuser-input

Can you reuse if statements? Program loops to only first if loop


This program is made to calculate the hypotenuse of a triangle by taking values of sides A and B. If the program is run, it offers 4 choices to the user, and nothing will happen until the user enters either A,B,C, or Q. Lets say the user enters A, it allows the user to enter the value of side A, and it will return to the 4 main choices. Problem is, when the user enters A,B,C, or Q, it just loops to ask for side A again. Below is the code I have currently. How do I make it so it doesn't only accept value A? Thanks

public class Newest_Exercise_1 {

     public static void main(String[] args) {    
         Scanner input = new Scanner(System.in);
         char letter;
         int valueOfA;
         int valueOfB;
         double hypotenuse;


         Boolean loop =true;

         letter = GettingUserInput(input);
            System.out.println("value of letter: " +letter);

         while(loop){
         if(letter=='A' || letter =='a'){
                valueOfA = InputWasA();
                System.out.println(valueOfA);
                letter = GettingUserInput(input);


         }
         if(letter=='B' || letter =='b'){
                valueOfB = InputWasB();
                System.out.println(valueOfB);
                letter = GettingUserInput(input);

         }

         if(letter=='C' || letter =='c'){
                System.out.println("made it to C");
                hypotenuse = Math.sqrt((valueOfA*valueOfA)+(valueOfB*valueOfB));
                System.out.println("Hypotenuse is: "+hypotenuse);
                letter = GettingUserInput(input);


         }
         if(letter=='Q' || letter =='q'){
                System.out.println("made it to Q");
                System.out.println("Program Closed");
                System.exit(0);
         }  
         }
     }

     public static char GettingUserInput(Scanner input){


            System.out.println("A-Enter value of side A");
            System.out.println("B-Enter value of side B");
            System.out.println("C-Calculate");
            System.out.println("Q-Quit the program");

            String s = input.next();
            System.out.println("value of s: "+s);
            char letter = s.charAt(0);

            while(letter != 'A' && letter != 'a' && letter != 'B' && letter != 'b' && letter != 'C' && letter != 'c' && letter != 'Q' && letter != 'q'){
                System.out.println("Invalid entry, Please try again");
                System.out.println("A-Enter value of side A");
                System.out.println("B-Enter value of side B");
                System.out.println("C-Calculate");
                System.out.println("Q-Quit the program");
                s = input.next();
                letter = s.charAt(0);
            }


        return letter; 
     }

     public static int InputWasA(){
         Scanner input = new Scanner(System.in);
         Boolean loop1 = true;
         int valueOfA = 0;

         while(loop1){
             try{
                 System.out.println("Enter value of side A");
                 valueOfA=input.nextInt();
                 loop1 = false;
             }
             catch(Exception e){
                 System.out.println("That was not an integer!");
                 input.next();
             }

         }
         return valueOfA;
     }

     public static int InputWasB(){
         Scanner input = new Scanner(System.in);
         Boolean loop2 = true;
         int valueOfB = 0;

         while(loop2){
             try{
                 System.out.println("Enter value of side B");
                 valueOfB=input.nextInt();
                 loop2 = false;
             }
             catch(Exception e){
                 System.out.println("That was not an integer!");
                 input.next();
             }

         }
         return valueOfB;
     }

     public static void InputWasC(){
            System.out.println("Made it to InputWasC!");
     }





}

Solution

  • You're only executing letter = GettingUserInput(input) once, so of course letter will be the same value forever.

    So:

    • Move this line inside the loop at the start
    • Don't repeat it anywhere else - it gets run now at the top of every iteration
    • and scrap the loop variable too, just use true

    Make the code look like:

    while (true) {
        letter = GettingUserInput(input);
        System.out.println("value of letter: " + letter);
    
        if (letter=='A' || letter =='a') {
            valueOfA = InputWasA();
            System.out.println(valueOfA);
        }
    
        // etc
    

    I have resisted suggesting other improvements, keeping just to the one causing the stated problem, however...

    Since your loop has:

    • An initial state (read letter)
    • A termination test (not a "q")
    • An iterative operation (read another letter)

    it is clearest and best coded as a for loop. Also, to avoid all those dual case tests, the GettingUserInput() method should only return lowercase letters, ie its last line should be:

    return Character.toLowerCase(letter); 
    

    Given that change, here's how I believe your loop should look:

    for (char letter = GettingUserInput(input); letter != 'q'; letter = GettingUserInput(input)) {
        if (letter =='a') {
            valueOfA = InputWasA();
            System.out.println(valueOfA);
         }
    
         if (letter =='b') {
             valueOfB = InputWasB();
             System.out.println(valueOfB);
         }
    
         if (letter =='c') {
             System.out.println("made it to C");
             hypotenuse = Math.sqrt((valueOfA*valueOfA)+(valueOfB*valueOfB));
             System.out.println("Hypotenuse is: "+hypotenuse);
         }
    }
    
    System.out.println("Program Closed");
    

    Note how the code for letter q is simply whatever follows the loop.