Read java strings within a while or do-while repetition structure

Asked

Viewed 1,378 times

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();
    }
}
  • 1

    What mistake? Edit the question explaining .

  • 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 a char

2 answers

1


Try it like this:

package listas;

import java.util.Scanner;

public class Listas {

    public static void main(String[] args) {
        Scanner ler = new Scanner(System.in);

        while (true) {            
            System.out.println("0-Sair");
            System.out.println("1-Cadastrar");
            System.out.println("2-Listar");
            System.out.println("3-Remover");
            String op = ler.nextLine().trim();
            switch (op) {
                case "0":
                    return;
                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 = ler.nextLine();
    }
}

Remember that from Java 7, Strings can be used in switches.

Also, avoid using the System.exit. This is bad programming practice.

The ler.nextByte() does not do what you expect. So use the ler.nextLine().

  • What is the alternative to System.exit?

  • 1

    @Sorack Usar return within the main.

0

Instead of

Scanner ler = new Scanner (System.in);

// ...

char op = (char) ler.nextByte();

you could use

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// ...

String s = br.readLine();

.

More details and implementation options on: Java: How to get input from System.console()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.