Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Java Error Help

  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Error Help

    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!

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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:

    Code:
    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:

    Code:
    String accountNumber = args[0];
    This is assuming that the first parameter to your program is the account number.

  • #3
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java Help

    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

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Either create an empty constructor in Account class

    Code:
    public Account() { }
    Or instantiate Account object with appropriate constructor

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •