PDA

View Full Version : Java Error Help


lisabailey
01-27-2009, 09:26 PM
I need help in fixing an error I am getting when I try to run my Java test program.

The code I have is:

public class AccountTest {


public static void main(String[] args) {
String AccountNumber;
Integer AccountType;
Double InterestRate;
Double Balance;


Account myAccount = new Account (AccountNumber, AccountType, InterestRate, Balance);
System.out.println(myAccount.displayInfo());

myAccount.setAccountNumber(AccountNumber);
myAccount.setAccountType(AccountType);
myAccount.setInterestRate(InterestRate);
myAccount.setBalance(Balance);


The error I am getting is:

The constructor Account(String, Integer, Double, Double) is undefined

I am trying to pull the answers that have been input on my main program.

I know its probably something simple I am over looking but I cant figure it out. Can anyone help me with this?

Thanks!

brad211987
01-27-2009, 11:39 PM
The error is telling you that your Account class does not have a constructor that matches the data you are trying to create it with. Since you are setting all of your values after creating the new Account, you probably needed to use the default constructor like this:

Account myAccount = new Account ();

This is just a guess, as I haven't seen your Account class. Also, if you are trying to access the command line arguments, you will need to use the args string array that is passed as an argument to the main method. Something like:

String accountNumber = args[0];

This is assuming that the first parameter to your program is the account number.

lisabailey
01-28-2009, 12:31 AM
Thank you soo much for your help. I tried correcting the code to just have

Account myAccount = new Account ();

but now it gives me the following error:

The constructor Account() is undefined

The code from my Account class is:

import java.util.Scanner;

public class Account {

private double balance;
private String accountNumber;
private int accountType;
private double interestRate;

Scanner input = new Scanner(System.in);

public Account (Double aBalance, String aAccountNumber,int aAccountType,double aInterestRate){
//over-riding the default constructor to make rules


setAccountNumber(aAccountNumber);
setAccountType(aAccountType);
setInterestRate(aInterestRate);
}// end constructor

public double getBalance() {
return balance;
}//end method getBalance

public void setBalance(double balance) {
this.balance = balance;
}// end method setBalance

public String getAccountNumber() {
return accountNumber;
}//end method getAccountNumber

public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}//end method setAccountNumber

public int getAccountType() {
return accountType;
}// end method getAccountType

public void setAccountType(int accountType) {
this.accountType = accountType;
}//end method setAccountType

public double getInterestRate() {
return interestRate;
}// end method getInterestRate

public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}//end method setInteresstRate

public void inputbalance(double balance){
System.out.print("Enter beginning balance: ");
balance = input.nextDouble();
}//end method inputbalance

public void inputAccountNumber(){
System.out.print("Enter Account Number: ");
accountNumber = input.next();
}//end method inputAccountNumber

public void inputAccountType(){
System.out.print("What is your Account Type(1,2,3): ");
accountType = input.nextInt();
}//end method inputAccountType

public void calculateInterestRate(){
interestRate = .03;
}// end method calculateInterestRate

public void calculatewithdrawl(double anAmount){
System.out.print("Enter amount of withdrawl: ");
anAmount = input.nextDouble();
balance = balance - anAmount;
}// end method calculatewithdrawl

public void calculatedeposit(double anAmount){
System.out.print("Enter amount of deposit: ");
anAmount = input.nextDouble();
balance = balance + anAmount;
}// end method calculatedeposit

public void calculatebalance(){
System.out.print("Balance is:" + balance);
}// end method calculatebalance

public String displayInfo(){
return "Account Number: " + getAccountNumber() + "Account Type: " +
getAccountType() + "Interest Rate: " + getInterestRate()+ "Balance: "
+getBalance ();
}// end method displayInfo
}// end class Account

brad211987
01-28-2009, 05:17 AM
You still need an empty constructor for this to work. Create a new constructor that accepts no arguments and sets the instance variables in the Account class to some default values.

servlet
01-28-2009, 06:56 AM
Either create an empty constructor in Account class


public Account() { }


Or instantiate Account object with appropriate constructor


Account account = new Account (aBalance, aAccountNumber, aAccountType, aInterestRate);