1

This is a part of my code(there is a BankAccount class after):

import java.util.Scanner; //This library is added

public class BankSystem_
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);//Scanner object to read input

        BankAccount ba = null;


        //Define variables
        int task=0;
        int accountNumber=0;
        String accountName="unknown";
        double balance=0;
        double interestRate=.05;

        do
        {       
        System.out.println("1.\t Open new account");
        System.out.println("2.\t Current Balance");
        System.out.println("3.\t Deposit");
        System.out.println("4.\t Withdrawal");
        System.out.println("5.\t Change Interest Rate");
        System.out.println("6.\t Bank Statement");
        System.out.println("7.\t Exit");
        System.out.println("Please create an account and choose a task: ");
        task=keyboard.nextInt();
            switch (task)
            {
                case 1:
                    System.out.print("What is the account name?: ");
                    accountName=keyboard.nextLine();
                    System.out.print("\nWhat is the account number?: ");
                    accountNumber=keyboard.nextInt();
                    System.out.print("\nAmount of money to start your account?: ");
                    balance=keyboard.nextDouble();
                        while (balance < 100)
                        {
                            System.out.println("The minimum amount to open a new account should be $100");
                            System.out.print("Amount of money to start your account?: ");
                            balance=keyboard.nextDouble();
                        }   
                    BankAccount ba = new BankAccount(accountName,accountNumber,balance,interestRate);
                    System.out.print(ba.NewAccount());  
                    break;
                case 2:
                    if (ba != null)
                    {
                    System.out.print(ba.Balance());
                    }
                    break;
                case 3:
                    if (ba != null)
                    {
                    System.out.print(ba.Deposit());
                    }
                    break;
                case 4:
                    if (ba != null)
                    {
                    System.out.print(ba.Withdrawal());
                    }
                    break;
                case 5: 
                    if (ba != null)
                    {       
                    System.out.print(ba.ChangeInterestRate());
                    }
                    break;
                case 6:
                    if (ba != null)
                    {
                    System.out.print(ba.BankStatement());
                    }
                    break;
                case 7:
                    System.exit(0);
                    break;
            }
        }while(task >=1 && task <=7);
    }
}

*The purpose of the code is to warn users who choose task 2-6 before choosing task 1. So, I have to check to see if the object of BankAccount is not null to continue.

Whenever I include:

BankAccount ba = null;

and I include later on in the code:

BankAccount ba = new BankAccount(accountName,accountNumber,balance,interestRate);

to declare the object, the compiler tells me that variable ba is already defined in method main(String[])

But when I remove the null, the compiler tells me :

variable ba might not have been initialized if (ba != null)

Do I have something wrong here?

luk2302
  • 55,258
  • 23
  • 97
  • 137

3 Answers3

1

Just change

BankAccount ba = new BankAccount(accountName,accountNumber,balance,interestRate);

to

ba = new BankAccount(accountName,accountNumber,balance,interestRate);

since you want to change the content of ba, which as a variable has already been defined at the beginning of your method. therefore you cant declare a variable with the same name again, you want to change the already exsiting one.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Also, i also have a problem that the program completely skips over allowing the user to input the account name. It will display the "What is the account name" but will go straight to "What is the account number?" –  Jul 06 '13 at 23:14
  • You are missusing `nextLine()`! Have you read its documentation? – luk2302 Jul 06 '13 at 23:20
  • okay, found a fix: put `keyboard.nextLine();` before you read into `accountName`: `keyboard.nextLine(); accountName=keyboard.nextLine();` [Click here for reference](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – luk2302 Jul 06 '13 at 23:28
0
BankAccount ba = new BankAccount(accountName,accountNumber,balance,interestRate);

tries to redeclare ba which is already declared.

Simply change it to:

ba=new BankAccount(accountName,accountNumber,balance,interestRate);

Also, you should declare BankAccount ba = null; as just BankAccount ba;. ba will be null in this case anyway.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

instead of using

BankAccount ba = new BankAccount(accountName,accountNumber,balance,interestRate);

use :

ba = new BankAccount(accountName,accountNumber,balance,interestRate);`

pouyan
  • 3,445
  • 4
  • 26
  • 44