Error reading strings - getting buffer \ n

1

I am using a variable of type String to pick up the answer whether or not to insert a new account.

When I start the program, in do while I ask for the name of the holder. The program does not display the question after reading the name of the holder, that is, I informed the holder, I typed enter, nothing appears on the console. I have to hit enter again to display the question.

So, I ask if you want to insert another account and after reading s or S , I have to type a second enter to ask for the name of the account holder. I am clearing the buffer after entering the name and after answering the question. Here are the sources:

public class ContaCorrente {    

    private int numero;
    private double saldo;
    private String titular;

    private static int numeroReferencia = 0;

    public ContaCorrente(String titular){  
        this.atribuirNumero();  
        this.titular = titular;  
        this.saldo = 0;  
    }  

    public void depositar (double valor) {  
        this.saldo += valor;  
    }  

    public void sacar (double valor) {  
        this.saldo = this.saldo - valor;  
    }  

    public double consultarSaldo () {  
        return this.saldo;  
    }  

    public void atribuirNumero () {  
        numeroReferencia ++ ;  
        this.numero = numeroReferencia;  
    }  

    public void detalharConta () {  
        System.out.println("Conta " + numero);  
        System.out.println("Titular :" + titular);  
    }  

}  

other class :

import java.util.Scanner;    
public class ContaCorrentePrincipal {

    private static Scanner entrada = new Scanner(System.in);    
    private static final int QUANTIDADE_CONTAS = 3;

    public static void main (String args[]) {

        ContaCorrente[] contas = new ContaCorrente[QUANTIDADE_CONTAS];

        String resposta;

        do {  
            int i = 0;  
            contas[i] = criarConta();     
            System.out.println("Quer continuar inserindo contas?");  
            resposta = entrada.nextLine();      
            // limpar buffer  
            entrada.nextLine(); 
            i ++ ;  
        }while ((resposta.equals("s")) || (resposta.equals("S")));  
    }  

    private static ContaCorrente criarConta () {  
        String titular;      
        System.out.println("Informe o nome do titular");  
        titular = entrada.nextLine();      
        // limpar buffer  
        entrada.nextLine();      
        ContaCorrente conta = new ContaCorrente(titular);  

        return conta;  
    }
}
    
asked by anonymous 23.03.2016 / 13:38

1 answer

1
   '    do {  
            int i = 0;  
            contas[i] = criarConta();     
            System.out.println("Quer continuar inserindo contas?");  
            resposta = entrada.nextLine();      
            // limpar buffer  
            entrada.nextLine(); 
            i ++ ;  
        } while ((resposta.equals("s")) || (resposta.equals("S")));  
    }  

    private static ContaCorrente criarConta () {  
        String titular;      
        System.out.println("Informe o nome do titular");  
        titular = entrada.nextLine();      
        // limpar buffer  
        entrada.nextLine();      
        ContaCorrente conta = new ContaCorrente(titular);  
        return conta;  
    }
}'

In all cases where you put entrada.nextLine() put only entrada.next()

    
23.03.2016 / 16:31