Java build error (Eclipse) using Scanner class, beginner problem

Asked

Viewed 244 times

0

I’m starting to study Java now for the book "Java How to Program 10 Edª" and I’m having trouble compiling this book code in Eclipse ( Follow the Error Image ).

Follow the code below

Could someone tell me how to make this code compile ?

Note: JDK 9, Java 9, Eclipse Oxygen 4.7.2

Erro quando compilo o seguinte código

public class Account {
  private String name;
  private double balance;

  public Account (String name, double balance) {
    this.name = name;

    if (balance > 0.0)
      this.balance = balance;
  }// Fim metodo


  public void deposit (double depositAmount) {
    if (depositAmount > 0.0)
      balance = balance + depositAmount;
  }//fim metod0

  public double getBalance () {
    return balance;
  }//fim get

  public void setName (String name) {
    this.name = name;
  }//fim set

  public String getName () {
    return name;
  }//fim get
}


public class AccountTest {


  public static void main(String[] args) {
    // Cria os objetos e atribui argumentos para o cosntrutor
        Account account1 = new Account("Jane Gree", 50.00);
        Account account2 = new Account("John Blue", -7.53);

        // Exibe o saldo inicial
        System.out.printf("% balance: $%.2f %n", account1.getName(), account1.getBalance());
        System.out.printf("% balance: $%.2f  %n%n", account2.getName(), account2.getBalance());

        Scanner input = new Scanner (System.in);

        // Obtendo a entrada na conta 1
        System.out.print("Enter deposit amount for account1: ");
        double depositAmount = input.nextDouble();
        System.out.printf("%n adding %.2f to account1 balance %n%n", depositAmount);
        account1.deposit(depositAmount);

        // exibe os saldos
        System.out.printf("%s balance: $%.2 %n", account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2 %n%n", account2.getName(), account2.getBalance());

        // Obtendo a entrada na conta 2
        System.out.print("Enter deposit amount for account2: ");
        depositAmount = input.nextDouble();
        System.out.printf("%nadding %.2f to account2 balance %n%n", depositAmount);
        account2.deposit(depositAmount);
        // exibe os saldos
        System.out.printf("%s balance: $%.2 %n", account1.getName(), account1.getBalance());
        System.out.printf("%s balance: $%.2 %n%n", account2.getName(), account2.getBalance());
        input.close();

  }

}
  • The problem is in formatting your System.out on line 12 of AccountTest. Change to System.out.printf("%s balance: $%.2f", account1.getName(), account1.getBalance());

  • 1

    Hey, Lucas! Welcome to Sopt. Whenever you put an error or even a code in your question (or answer), avoid using images for this, as the next people looking for the same mistake in the future will not find your question since the error code will be in the image and not in a text.

  • Another thing, as in this case there is an indicator of which line has the defect, when pasting the code here, make a comment indicating which line the indicator shows. The log there indicates line 12, but what is line 12?

  • Look thanks for the help, and for the advice on the next question I put, as advised. And it worked out just put %s in my code hahaha

No answers

Browser other questions tagged

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