Help with Java exercise

I am having a little problem in a JAVA exercise, I can not arrange, although I am almost sure that it is very simple. It is a simple crud:

Below is the main code

import java.util.Scanner;

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

        int max = 4;

        Conta conta[] = new Conta[max];
        Conta c = new Conta();

        int op;
        int indice = 0;

        do{
            System.out.println("1 - criar conta");
            System.out.println("2 - consultar conta");
            System.out.println("7 - sair");
            System.out.print("escolha: ");
            op = scan.nextInt();

            switch(op){
                case 1:
                    if(indice < max){
                        c = new Conta();
                        System.out.println("Número da conta: ");
                        String num = scan.nextLine();
                        c.setNumero(num);
                        scan.nextLine();

                        System.out.println("Saldo: ");
                        double saldo = scan.nextDouble();
                        c.setSaldo(saldo);

                        conta[indice] = c;
                        indice++;
                    }else{
                        System.out.println("");
                        System.out.println("Número limite total de contas atingido!");
                        System.out.println("");
                    }
                    break;

                case 2:
                    if(indice >= 0){
                        System.out.print("Digite o número da conta por favor: ");
                        String busca = scan.nextLine();
                        scan.nextLine();

                        for(int i = 0; i <= indice; i++){
                            if(busca.equals(conta[i].getNumero())){
                                int achou = i;
                                System.out.println("número - " + conta[achou].getNumero());
                                System.out.println("saldo - " + conta[achou].getSaldo());
                            }
                        }
                    }else{
                        System.out.println("");
                        System.out.println("Nenhuma conta cadastrada no momento...");
                        System.out.println("");
                    }
                    break;
                default:
                    System.out.println("Opção inválida");
            }

        }while(op != 7);
    }
}

Here is the Code of the account Class:

public class Conta{
    private String numero;
    private double saldo;

    public String getNumero(){
        return numero;
    }

    public void setNumero(String numero){
        this.numero = numero;
    }

    public double getSaldo(){
        return saldo;
    }

    public void setSaldo(double saldo){
        this.saldo = saldo;
    }
}

I am not able to show the account number only the balance and I am not able to fetch the account by the number that is Option 2.

Author: Henrique Nascimento, 2017-01-02

2 answers

Happens because your first Scanner#nextInt() does not consume the last newline of the expression, it only consumes the integer value and leaves the newline unchanged. So the nextLine that vc assigns to the variable num returns empty. And what is stopping the application is the second nextLine that does not attribute to anything. It is something already known in the java scenario, but they always hit it.

The solution would be you consume that newline right after the Scanner#nextInt:

System.out.println("1 - criar conta");
System.out.println("2 - consultar conta");
System.out.println("7 - sair");
System.out.print("escolha: ");
op = scan.nextInt(); 

Or else, take as String and convert to Integer, which I I advise, because you never know what the user will Type:

System.out.println("1 - criar conta");
System.out.println("2 - consultar conta");
System.out.println("7 - sair");
System.out.print("escolha: ");
try {
    op = Integer.parseInt(scan.nextLine());
} catch (NumberFormatException e) {
    e.printStackTrace();
    // Ou fazer algo a mais.
}
 1
Author: Gustavo Cinque, 2017-01-02 17:12:26

So, the problem is the following, you are not being able to consult because it is not actually saving the account number, try to create several accounts and you will see that it will show all, because as there is nothing in C. getnumero the comparison will continue and will show only the value of all the balances that have been saved. The problem is something quite simple, you use the next() method instead of nextLine () to get the next values in String, so you should do the following substitution:

 if(indice < max){
                    c = new Conta();
                    System.out.println("Número da conta: ");
                    String num = scan.nextLine();
                    c.setNumero(num);
                    scan.nextLine();

                    System.out.println("Saldo: ");
                    double saldo = scan.nextDouble();
                    c.setSaldo(saldo);

                    conta[indice] = c;
                    indice++;
                }

By

 if(indice < max){
                    c = new Conta();
                    System.out.println("Número da conta: ");
                  //  scan.nextLine();
                    String num = scan.next();
                    c.setNumero(num);

                    scan.nextLine();

                    System.out.println("Saldo: ");
                    double saldo = scan.nextDouble();
                    c.setSaldo(saldo);

                    conta[indice] = c;
                   System.out.println(num+saldo);
                    indice++;
                }

When saving, and when querying tbm:

Before:

       if(indice >= 0){
                    System.out.print("Digite o número da conta por favor: ");
                    String busca = scan.nextLine();
                    scan.nextLine();

                    for(int i = 0; i <= indice; i++){
                        if(busca.equals(conta[i].getNumero())){
                            int achou = i;
                            System.out.println("número - " + conta[achou].getNumero());
                            System.out.println("saldo - " + conta[achou].getSaldo());
                        }
                    }

After

  if(indice >= 0){
                    System.out.print("Digite o número da conta por favor: ");
                    String busca = scan.next();


                    for(int i = 0; i < indice; i++){
                        if(busca.equals(conta[i].getNumero())){
                            int achou = i;
                            System.out.println("número - " + conta[achou].getNumero());
                            System.out.println("saldo - " + conta[achou].getSaldo());
                        }
                    }

I noticed that I was returning an index error in the console, the problem was that i was on a larger index than the last stored, the solution is to just swap the

 1
Author: André Araujo, 2017-01-02 17:27:21