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!");

}

}

[3544 byte] By [tara.87.bisa] at [2007-11-26 17:49:27]
# 1

You have to give a local variable a value before you can use it. (Member variables are given default values during the object construction process.)

So if you do int ii;

System.out.println(ii);

the compiler will complain because it can't be sure the ii will have been given a value at the point you try to print it.

I haven't looked at your code and I don't know your requirements, so I can't tell you exactly what to do, but in general, if you're trying to use (e.g., print) a variable's value, you have to have assigned it a value. So at the point where the compiler's complaining, think about your logic, whether it makes sense to use that variable there, what its value should be, where it should be set, etc.

jverda at 2007-7-9 5:01:55 > top of Java-index,Java Essentials,New To Java...
# 2

> 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)

koushic_dka at 2007-7-9 5:01:55 > top of Java-index,Java Essentials,New To Java...
# 3
> So as per java you must initialize the variables to some default value before you can use themNear as I can tell from the code posted, is that he/she is already doing that. They don't have to be initialized on the same line as they are declared. Your advice is misleading.
warnerjaa at 2007-7-9 5:01:55 > top of Java-index,Java Essentials,New To Java...