javaeclipsebanking

New to programming. Building simple Banking Application - Syntax error on token ";", { expected. - Error in Eclipse (code included)


I'm new to coding and following a tutorial on YouTube for creating a simple Banking Application.

I'm getting an error: Syntax error on token ";", { expected

I've outlined where the error is in my code below.

Things I've tried:

  1. I've taken a look at the braces and they look fine to me.
  2. I've taken a look at the other similar questions on here and they don't seem to help.
  3. Google'd and still can't figure out what's causing this frustrating error.
  4. Google tells me it's something to do with brackets maybe but i can't see the problem?

Code Below:

import java.util.Scanner;

public class BankingApplication {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BankAccount obj1 = new BankAccount("Daniel", "BAB001");
        obj1.showMenu();
    }

}
class BankAccount {

    int balance;
    int previousTransaction;
    String customerName;
    String customerId;

    BankAccount(String cname, String cid) {

        customerName = cname;
        customerId = cid;
    }

    void deposit(int amount) {

        if (amount >= 0) {
            balance = balance + amount;
            previousTransaction = amount;
        }
    }

    void withdraw(int amount) {

        if (amount != 0) {
            balance = balance - amount;
            previousTransaction = amount;
        }
    }

    void getPreviousTransaction() {

        if (previousTransaction > 0) {
            System.out.println("Deposited: " + previousTransaction);
        } else if (previousTransaction < 0) {
            System.out.println("Withdrawn: " + Math.abs(previousTransaction));
        } else {
            System.out.println("No transaction occured");
        }
    }

    void showMenu() {

        char option= '\0';
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome "+customerName);
        System.out.println("Your ID: "+customerId);
        System.out.println("\n");
        System.out.println("A. Check Balance");
        System.out.println("B. Deposit");
        System.out.println("C. Withdraw");
        System.out.println("D. Previous Transaction");
        System.out.println("E. Exit");

        do {
            System.out.println("======================================================");
            System.out.println("Enter an option");
            System.out.println("======================================================");
            option = scanner.next().charAt(0);
            System.out.println("\n");
            {
        //error is on line directly under this one
            switch(option)

            case 'A':
                System.out.println("======================================================");
                System.out.println("Balance = "+balance);
                System.out.println("======================================================");
                System.out.println("\n");
                break;

            case 'B':
                System.out.println("======================================================");
                System.out.println("Enter an amount to deposit: ");
                System.out.println("======================================================");
                int amount = scanner.nextInt();
                deposit(amount);
                System.out.println("\n");
                break;

            case 'C':
                System.out.println("======================================================");
                System.out.println("Enter an amount to withdraw: ");
                System.out.println("======================================================");
                int amount2 = scanner.nextInt();
                withdraw(amount2);
                System.out.println("\n");
                break;

            case 'D':
                System.out.println("======================================================");
                getPreviousTransaction();
                System.out.println("======================================================");
                int amount3 = scanner.nextInt();
                deposit(amount3);
                System.out.println("\n");
                break;

            case 'E':
                System.out.println("*****************************************************");
                break;

            default:
                System.out.println("Invalid option: Please try again");
                break;

                }
        }

        while(option != 'E') ;
        System.out.println("Thank you for using our services");


        }
}

Solution

  • You need to encapsulate your switch statement in brackets (just like you would for a method):

    switch(option) {
    
    ...
    
    default:
        System.out.println("Invalid option: Please try again");
        break;
    
    }