Help with Java Exercise

Asked

Viewed 276 times

3

I’m having a little problem in a JAVA exercise, I can’t 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 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 find the account by the number that is option 2.

  • So man the problem is in your Dice, take a look at him that sure you can solve this one yourself.

2 answers

1

It happens because your first Scanner#nextInt() does not consume the last newline of the expression, only consumes the whole value and leaves the newline unchanged. Therefore the nextLine which you assign to the variable num returns empty. And what is stopping the application is the second nextLine that does not assign anything. It is something known already in the java scenario, but always hit it.

The solution would be for you to consume this newlineshortly 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 into 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.
}
  • Hi Gustavo, thank you so much for the answer, I will implement this tip in the other codes you do.

1

So the problem is this, you’re not being able to query it because it’s not actually saving the account number, try to create multiple accounts and you’ll see that it will show all, Since there is 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 the next() method instead of nextLine() to take the next values in String, then you should make the following substitution: this

 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++;
                }

for

 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 at the time of consulting:

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());
                        }
                    }

Afterward

  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 Indice error on the console, the problem was that i was staying on an Indice larger than the last stored, the solution is just to change the <= for just <.

  • 1

    Hello André! Thanks man, it worked, I had never noticed this issue of nextLine(), I will review some things, not wise that the scan.next(); it worked.

Browser other questions tagged

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