String error using the nextLine() method in Java

Asked

Viewed 436 times

1

I have a problem with a simple java program. When I run and scan with String, it simply goes straight to the end of the program, but when I put char it does not pass and runs normally, even so I wanted to run with strings and not with charUma imagem do que acontece. That is the code:

public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);

        String resp;
        int op;

        do{
        System.out.println("////////////////////BANCO/////////////////////////////");
        System.out.println("O que deseja?"+
                "\n[1]Para criar uma nova conta;"+
                "\n[2]Para sair.");
        op = in.nextInt();

        switch(op){
            case 1:
                System.out.println("Você deseja criar uma nova conta? [s/n]");
                resp = in.nextLine();
                if(resp.equals("s")){
                    Conta c1 = new Conta();
                    System.out.println("\n---------------------------------------");
                    System.out.println("Tipo da conta: ");
                    c1.abrirConta(in.nextLine());

                    System.out.println("\n[1]Para depositar;"+
                            "\n[2]Para sacar;"+
                            "\n[3]Para pagar mensalidade;"+
                            "\n[4]Para fechar conta;"+
                            "\n[5]Para sair.");
                    int op2 = in.nextInt();

                    switch(op2){
                        case 1:
                            System.out.println("Quanto você deseja depositar?");
                            c1.depositar(in.nextFloat());

                            break;
                        case 2:
                            System.out.println("Quanto você deseja sacar?");
                            c1.sacar(in.nextFloat());
                            break;
                        case 3:
                            System.out.println("Você deseja pagar a mensalidade? [s/n]");
                            String resp2 = in.nextLine();
                            if(resp2.equals("s")){
                            c1.pagarMensal();
                            }else if(resp2.equals("n")){
                                break;}
                            break;
                        case 4:
                            System.out.println("Você deseja fechar essa conta? [s/n]");
                            String resp3 =in.nextLine();
                            if(resp3.equals("s")){
                                c1.fecharConta();
                            }else if(resp3.equals("n")){break;}
                            break;
                        case 5:
                            System.out.println("Você deseja sair? [s/n]");
                            String resp4 = in.nextLine();
                            if(resp4.equals("s")){
                                break;
                            }else if(resp4.equals("n")){
                            }
                            break;
                        }
                    }
                break;
            case 2:
                System.out.println("Você deseja sair? [s/n]");
                String resp5 = in.nextLine();
                if(resp5.equals("s")){
                    break;
                }else if(resp5.equals("n")){

                }
            }
        }while(op == 2);
        }
}

1 answer

2


Use the next(); in place of nextLine();.

I ran a test, tried to replicate your code, just without the class methods Conta. He rode till the end of the deal.

public static void main(String[] args) {
    // TODO code application logic here
    Scanner in = new Scanner(System.in);

    String resp;
    int op;

    do{
    System.out.println("////////////////////BANCO/////////////////////////////");
    System.out.println("O que deseja?"+
            "\n[1]Para criar uma nova conta;"+
            "\n[2]Para sair.");
    op = in.nextInt();

    switch(op){
        case 1:
            System.out.println("Você deseja criar uma nova conta? [s/n]");
            resp = in.next();
            if(resp.equals("s")){
                System.out.println("Sim");

                int op2 = in.nextInt();

                switch(op2){
                    case 1:
                        System.out.println("Selecionado 1");
                        break;
                    case 2:
                        System.out.println("Selecionado 2");
                        break;
                    case 3:
                        System.out.println("Você deseja pagar a mensalidade? [s/n]");
                        String resp2 = in.next();
                        if(resp2.equals("s")){
                        System.out.println("Pagar mensalidade");
                        }else if(resp2.equals("n")){
                            break;}
                        break;
                    case 4:
                        System.out.println("Você deseja fechar essa conta? [s/n]");
                        String resp3 =in.next();
                        if(resp3.equals("s")){
                            System.out.println("Fechado");
                        }else if(resp3.equals("n")){break;}
                        break;
                    case 5:
                        System.out.println("Você deseja sair? [s/n]");
                        String resp4 = in.next();
                        if(resp4.equals("s")){
                            break;
                        }else if(resp4.equals("n")){
                        }
                        break;
                    }
                }
            break;
        case 2:
            System.out.println("Você deseja sair? [s/n]");
            String resp5 = in.next();
            if(resp5.equals("s")){
                break;
            }else if(resp5.equals("n")){

            }
        }
    }while(op != 2);
}

The while is declared wrong too, correct would be while(op != 2) as noted. The error you described I believe has nothing to do with this directly, with the nextLine(); the application really skips the line.

It seems that it happens because you are reading the next whole first with the nextInt(), only that the user besides writing a number he has to give a ENTER to validate the entry. This enter is actually an n character, so as you read only the integer that n will be in the input buffer. So, your next statement nextLine() will consume this n

  • You can delete your comments to clear this section and not confuse.

  • 1

    Now it worked! Thank you!

Browser other questions tagged

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