Help with Java exercise

3

I have a problem with a JAVA exercise, I can not fix it, although I'm pretty sure it's pretty simple. It's 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 for 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 can not show the account number only the balance and I can not get the account by the number that is option 2.

    
asked by anonymous 02.01.2017 / 17:30

2 answers

1

It 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% w / o% you assign to the% variable with% returns empty. And what is stopping the application is the second nextLine that does not attribute to anything. It's something already known in the java scenario, but they always hit it.

The solution would be for you to consume this num shortly after nextLine :

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, take as String and convert to Integer, which 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.
}
    
02.01.2017 / 18:12
1

So, the problem is this, you are not being able to consult because it is not actually saving the account number, try creating multiple accounts and you will see that it will show them all, because you have nothing in c.getNumber the comparison will continue and show only the value of all balances that have been saved. The problem is something very simple, you use the next () method instead of nextLine () to get the next values in String, then 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++;
                }

At the time of saving, and in the when consulting 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());
                        }
                    }

Then

  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 on the console, the problem was that i was at a higher index than the last stored, the only solution is to change the

02.01.2017 / 18:27