Tyler Bull Tyler Bull - 2 months ago 7
Java Question

Do While Loop does not recognize scanner input before while loop is tested

import java.util.Scanner;

public class Switch {


private static void case1() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side A");
String a = input.next();

System.out.println("Enter side B");
String b = input.next();

double number1 = Double.parseDouble(a);
double number2 = Double.parseDouble(b);


double A2 = number1 * number1;
double B2 = number2 * number2;

System.out.println(A2);
System.out.println(B2);

double Csq = A2 + B2;

double C = Math.sqrt(Csq);

System.out.println("C equals " + C);
input.close();
}

private static void case2() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side A");
String a = input.next();

System.out.println("Enter side C");
String c = input.next();

double number1 = Double.parseDouble(a);
double number2 = Double.parseDouble(c);


double A2 = number1 * number1;
double C2 = number2 * number2;

System.out.println(A2);
System.out.println(C2);

double b1 = C2 - A2;

double B = Math.sqrt(b1);

System.out.println("B equals " + B);
input.close();
}

private static void case3() {
Scanner input = new Scanner(System.in);
System.out.println("Enter side B");
String b = input.next();

System.out.println("Enter side C");
String c = input.next();

double number1 = Double.parseDouble(b);
double number2 = Double.parseDouble(c);


double B2 = number1 * number1;
double C2 = number2 * number2;

System.out.println(B2);
System.out.println(C2);

double a1 = C2 - B2;

double A = Math.sqrt(a1);

System.out.println("A equals " + A);
input.close();
}


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String cont = "y";

do {

try{
System.out.println("Welcome to the Pythagorean Theorm Program!!");
System.out.println("");
System.out.println("Choose one of the following:");
System.out.println("");
System.out.println("1. A2 + B2 = X");
System.out.println("2. A2 + X = C2");
System.out.println("3. X + B2 = C2");

int choice = input.nextInt();

switch (choice) {

case 1: case1();
break;
case 2: case2();
break;
case 3: case3();
break;
}
}
catch(Exception e) {
System.out.println("error");
}

System.out.println("Do you want to continue?");
String more = input.next();
} while (more.equals(cont));



}
}


I need a user input just before the while loop that way it will know if the program wants to continue. Problem is that the more inside of the while condition does not see the scanner input a line before. Any suggestions?

Answer

put the String more declaration outside the while loop :

`public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String cont = "y";
        String more = null;


        do {

            try{
                System.out.println("Welcome to the Pythagorean Theorm Program!!");
                System.out.println("");
                System.out.println("Choose one of the following:");
                System.out.println("");
                System.out.println("1. A2 + B2 = X");
                System.out.println("2. A2 + X = C2");
                System.out.println("3. X + B2 = C2");

                int choice = input.nextInt();

                switch (choice) {

                    case 1: case1();
                            break;
                    case 2: case2();
                            break;
                    case 3: case3();
                            break;
                }
            }
            catch(Exception e) {
                System.out.println("error");
            }

            System.out.println("Do you want to continue?");
            more = input.next();
        } while (more.equals(cont));



    }`
Comments