Vairables not initialised
Hi,
Sorry if this has been posted before. Have a problem with the following code. It's for a lab assignment for college and we were given the driver program and the class definition(except we had to add in bits and peices). Here, depending on which option(name supplied, name and balance supplied etc) it tells me that the variables name and balanace have not been initialised. Problem is I don't really know what to initialise them to...if anything?
Any help would be appriciated =)
//*******************************************************
// TestAccount.java
//
// A simple driver to test the overloaded methods of
// the Account class.
//*******************************************************
import java.util.Scanner;
publicclass TestAccount
{
publicstaticvoid main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct;
Scanner scan =new Scanner(System.in);
//constructor with only name supplied, number randomly generated
System.out.println("Enter account holder's first name");
name = scan.next();
acct =new Account(name);
System.out.println("Account for " + name +":");
System.out.println(acct);
//constructor with balance and name supplied, number randomly generated
System.out.println("\nEnter initial balance");
balance = scan.nextDouble();
acct =new Account(balance, name);
System.out.println("Account for " + name +":");
System.out.println(acct);
//constructor with all 3 arguments supplied
System.out.println("\nEnter account number");
acctNum = scan.nextLong();
acct =new Account(balance, name, acctNum);
System.out.println("Account for " + name +":");
System.out.println(acct);
//demonstrating method calls
System.out.print("\nDepositing €100 into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing €25, balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print("\nWithdrawing €25 with €2 fee, balance is now ");
acct.withdraw(25, 2);
System.out.println(acct.getBalance());
System.out.println("\nBye!");
}
}
> Hi,
> Sorry if this has been posted before. Have a problem
> with the following code. It's for a lab assignment
> for college and we were given the driver program and
> the class definition(except we had to add in bits and
> peices). Here, depending on which option(name
> supplied, name and balance supplied etc) it tells me
> that the variables name and balanace have not been
> initialised. Problem is I don't really know what to
> initialise them to...if anything?
>
> Any help would be appriciated =)
>
> > //****************************************************
> ***
> // TestAccount.java
> //
> // A simple driver to test the overloaded methods of
>
> // the Account class.
> //****************************************************
> ***
> import java.util.Scanner;
> public class TestAccount
> {
> public static void main(String[] args)
> {
> String name;
> double balance;
> long acctNum;
> Account acct;
> Scanner scan = new Scanner(System.in);
>
> //constructor with only name supplied, number
> mber randomly generated
> System.out.println("Enter account holder's first
> st name");
> name = scan.next();
> acct = new Account(name);
> System.out.println("Account for " + name + ":");
> System.out.println(acct);
>
> //constructor with balance and name supplied,
> d, number randomly generated
> System.out.println("\nEnter initial balance");
> balance = scan.nextDouble();
> acct = new Account(balance, name);
> System.out.println("Account for " + name + ":");
> System.out.println(acct);
>
>//constructor with all 3 arguments supplied
> System.out.println("\nEnter account number");
> acctNum = scan.nextLong();
> acct = new Account(balance, name, acctNum);
> System.out.println("Account for " + name + ":");
> System.out.println(acct);
>
> //demonstrating method calls
> System.out.print("\nDepositing €100 into account,
> t, balance is now ");
> acct.deposit(100);
> System.out.println(acct.getBalance());
> System.out.print("\nWithdrawing €25, balance is now
> ow ");
> acct.withdraw(25);
> System.out.println(acct.getBalance());
> System.out.print("\nWithdrawing €25 with €2 fee,
> e, balance is now ");
> acct.withdraw(25, 2);
> System.out.println(acct.getBalance());
> System.out.println("\nBye!");
>
> }
> }
>
The Variables you have used
String name;
double balance;
long acctNum;
Account acct;
are declared in within a method (The Main Method ) . So as per java you must initialize the variables to some default value before you can use them
Eg > String name="XXX"
double balance=100.1111111
long acctNum=123456
Account acct=null or Account acct=new Account()
This will get rid of the compiler errors. Make sure u initialize variables used within any method(Not Main method alone)