Do While is not working properly

Asked

Viewed 89 times

-1

Do while is not working in the code. At first the test that I am doing now is only the first of the menu (register the user) and then should go back to the menu until S is typed. (It is running only one time)
Another question is how to put the registered in a list?

 import java.util.ArrayList;
 import java.util.Scanner;
 public class CadastraUsuarios {
 Scanner entrada;

    public void menu(){

    entrada = new Scanner(System.in);
    ArrayList<Clientes> listaDeUsuarios = new ArrayList<Clientes>();

    int op = 0,i,j;


    String opcao;
     do{
    System.out.println("MENU DE ESCOLHAS");
    System.out.println("A- CADASTRAR CLIENTE");
    System.out.println("B- INSERIR O CADASTRO DE UM NOVO TIPO ");
    System.out.println("C- LISTAR TODOS OS  CADASTRADOS");
    System.out.println("D- LISTAR TODOS OS CLIENTES CADASTRADOS");
    System.out.println("E- LISTAR OS  CADASTRADOS PARA UM DETERMINADO CLIENTE");
    System.out.println("F- BUSCAR INFORMAÇÕES DE UM PRODUTO PELO NÚMERO DE ");
    System.out.println("S- SAIR");

    opcao=entrada.nextLine().toUpperCase().trim();
    switch(opcao){
        case "A": System.out.println("Cadastro de Cliente:");


        System.out.println("Digite o nome do usuário");
        Clientes cliente = new Clientes(entrada.nextLine()); 
        System.out.println(cliente.getNome());

        //cadastrar clientes
        break;
        case "B": System.out.println("Inseririndo o cadastro de um novo tipo ...");
        //inserir o cadastro de um novo tipo de 
        break;
        case "C" : System.out.println("Listando todos os  cadastrados...");
        //listar todos os  cadastrados

        break;
        case "D" : System.out.println("Listando todos os clientes cadastrados...");
        //listar todos os clientes cadastrados

        break;
        case "E" : System.out.println("Listando os  cadastrados para um determinado cliente...");
        //listar os cadastrados para um determinado cliente

        break;
        case "F" : System.out.println("Buscando as informações   pelo número ...");
        //buscar as informações  pelo número 

        break;
        case "S" : System.out.println("saindo do programa...");
        //sair do programa

        break;



    }
}while(opcao=="S");

}

//String nome = scanner.nextLine(); cliente.setNome(nome);
}
  • 1

    As an additional note this code is an example of how to do the opposite of object orientation.

  • @Maniero I think is an example of college, when they are teaching data entry in Java, it is quite common nowadays (and also it was when I graduated).

  • @Maniero is an example of college, I still do not have much affinity with object-oriented programming. Usually an error of this type appears. I’m sorry ehehe.

1 answer

2


When comparing strings, use the method equals(). Yeah, the comparators == will compare to object references (if they are the same object).

The method equals() will compare whether the value of an object is the same as the value passed as parameter.

Therefore, it would be:

while(opcao.equals("S"));

However, I imagine that your code should repeat as long as the "S" option is not chosen. Therefore, your comparison should be a denial:

// enquanto não for "S", continue o loop
while(!opcao.equals("S"));

More information:

https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

Browser other questions tagged

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