/ / Obtendo uma string de um scanner quando um número é inserido - java

Obtendo uma string de um scanner quando um número é digitado - java

Estou tentando escrever um programa que pede a um usuáriopara 2 números e, em seguida, peça ao usuário para escolher um comando de um menu digitando o número correspondente ao comando. Posso escrever o programa se considerar a entrada como um Int, mas não consigo descobrir como uma string, também tem que ser uma string. Estou tendo problemas quando ele entra no loop while para validar a entrada do usuário, ele não para quando a instrução é falsa, ele permanecerá no loop. Não consigo descobrir o que estou fazendo de errado. Aqui está o código que tenho.

import java.util.Scanner;
public class ab {
public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");
input = stdIn.next();
System.out.println();
while (!input.equals(1) && !input.equals(2) && !input.equals(3) && !input.equals(4)) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
System.out.println();
if (input.equals(1)) {
System.out.print(L + " * " + R + " = " + (L * R));
} else if (input.equals(2)) {
System.out.print(L + " / " + R + " = " + (L / R));
} else if (input.equals(3)) {
System.out.print(L + " + " + R + " = " + (L + R));
} else {
System.out.print(L + " - " + R + " = " + (L - R));
}
}
stdIn.close();
}
}

Qualquer ajuda seria muito apreciada. Agradeço antecipadamente.

Respostas:

0 para resposta № 1

A linha input = stdIn.next(); está recebendo entrada como String enquanto sua comparação é contra um inteiro. Então um String nunca é igual a Int

Você pode tentar alterar a condição do loop while para:

while (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))

observe as aspas duplas em torno dos números


0 para resposta № 2

Está respondido, mas verifique

import java.util.Scanner;

public class ab {

public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");

input = stdIn.next();

while (true) {

if (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4")) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
} else {
if (input.equals("1")) {
System.out.print(L + " * " + R + " = " + (L * R));
break;
} else if (input.equals("2")) {
System.out.print(L + " / " + R + " = " + (L / R));
break;
} else if (input.equals("3")) {
System.out.print(L + " + " + R + " = " + (L + R));
break;
} else {
System.out.print(L + " - " + R + " = " + (L - R));
break;
}
}

}


stdIn.close();
}

}