1
How do I read java strings within a while or do-while? repeat structure without error and reads
Code:
package listas;
import static java.lang.System.exit;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Listas {
//declaração de Variáveis
static List lista = new ArrayList();
static Scanner ler = new Scanner (System.in);
public static void main(String[] args) {
while (true) {
System.out.println("0-Sair");
System.out.println("1-Cadastrar");
System.out.println("2-Listar");
System.out.println("3-Remover");
char op = (char) ler.nextByte();
switch (op) {
case 0 : exit(0);break;
case 1 : cadastrar(); break;
case 2 : break;
case 3 : break;
default : {
System.out.println("Opção Inválida!");
break;
}
}
}
}
public static void cadastrar () {
System.out.print("Digite: ");
String str = new String ();
str = ler.nextLine();
}
}
What mistake? Edit the question explaining .
– user28595
char op = (char) ler.nextByte();
It doesn’t do what you expect it to do, and of course it’s not the right way to read achar
– Isac