Page 1 of 2 12 LastLast
Results 1 to 20 of 30
  1. #1
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default Calling a method in a different class from within a method problem

    Hello again everyone,

    I am having one last problem with the application I am working on. here is a brief scenario on what happens, A user inserts an accountID number, either a 1 or a 2, then it takes this accountID and checks an array and returns the accountID if it is found within the array.
    Now I need to take this accountID and generate the DebitCard class with the object. I am new to java but I believe we just call the method right? Here is the code from the method I am calling the DebitCard method from, Notice the comments on what I have tried. After that code I have pasted the DebitCard class, I need to call the DebitCard(Account account) method by using the accountID the user entered, any suggestions?

    public void askUserToInsertDebitCard() {

    Scanner input = new Scanner(System.in);

    //displays a message and sets the user output to the ID variable
    displayMessage("Please insert debit card" +
    " - Enter account ID (e.g., 1 or 2)");

    //Sets the currentAccountID integer to input
    currentAccountID = input.nextInt();

    //Calls getAcount() in AccountRepository to check the value with
    //the array of pre-existing accountID's
    account = AccountRepository.getInstance();
    account.getAcount(currentAccountID);

    //Generate a new DebitCard class with the account
    //object created earlier

    //These are lines of code I have tried to call DebitCard(Account account)
    //from the DebitCard class

    //account =DebitCard.DebitCard(account); didnt work

    //DebitCard myDebitCard = new DebitCard();
    //myDebitCard.DebitCard(account); did not work either

    //Any Ideas on how to accomplish this
    }

    The DebitCard class Code

    package coldbeveragejava;
    /*
    * DebitCard.java
    *
    * Created on October 21, 2006, 12:49 PM
    *
    * To change this template, choose Tools | Options and locate the template under
    * the Source Creation and Management node. Right-click the template and choose
    * Open. You can then make changes to the template in the Source Editor.
    */

    /**
    *
    * @author dan
    */
    public class DebitCard {
    private String pathToDebitCardImage = null; //for IT171
    private final int MAX_ACCOUNTS=2;
    private Account[] accounts = new Account[MAX_ACCOUNTS];
    private Account accountOnDebitCard;

    public DebitCard(Account account){
    accountOnDebitCard=account;
    }

    /** Creates a new instance of DebitCard */
    public DebitCard(String debitCardImage) {
    if(debitCardImage==null){
    System.out.println("No debit card image for IT170");
    }
    //put code in here in 171 for reading the debit card from disk

    //in a for loop initialize the accounts array to the following
    // account number 1 200 (cents)
    // account number 2 50 (cents)
    }//DebitCard


    //To be called from user interface
    public double debitAccount(Account account, int debitAmount){
    System.out.println("Entered DebitCard debitAccount "+ account.getAccountNumber()+" "+
    debitAmount);

    double returnAmount = 0.0;
    //look up account number in accounts and deduct the debitAmount
    //before deducting make sure that the amount that would result
    //is > 0.
    //If it is greater than 0 just debit the account and return the remaining
    //amount
    //If the amount is less than 0, do not debit the account, rather return
    //-1.0
    //message

    return returnAmount;
    }//DebitAccount


    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The DebitCard class has two constructors; both take a single argument:
    Java Code:
        public DebitCard(Account account)
    
        public DebitCard(String debitCardImage)
    To create an instance of DebitCard you would use one of these, eg,
    Java Code:
    //Generate a new DebitCard class instance with the account
    //object created earlier
    DebitCard debitCard = new DebitCard(Account account);
    Now you can use the reference, viz, "debitCard", to this instance to call the debitAccount method in DebitCard.

  3. #3
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Thats what I thought but its not working, Here is the code I used

    DebitCard debitCard = new DebitCard(Account account);
    debitCard.DebitCard(account);

    Both lines if code have red squiggles under them, the first line returns the follwing error

    ')' expected
    ';' expected
    not a statement
    cannot find symbol variable account

    and the second line returns this error

    cannot find symbol
    symbol: method DebitCard (coldbeveragejava.AccountRepository)
    location: class coldbeveragejava.DebitCard

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Java Code:
    cannot find symbol 
    symbol: method DebitCard (coldbeveragejava.AccountRepository)
    location: class coldbeveragejava.DebitCard
    This means that it can't find the class DebitCard. Make sure you either imported it, or it's in the same folder.

    Java Code:
    ')' expected
    ';' expected
    not a statement
    cannot find symbol variable account
    All these error pop-up because you use
    Java Code:
    new DebitCard(Account account)
    in which Account account is wrong. You should either initialize the variable account in a line before, and use
    Java Code:
    new DebitCard(account)
    or you should use
    Java Code:
    new DebitCard((Account account = new Account));
    Hopefully this was helpful.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Sorry, I made a mistake.
    This
    Java Code:
    //Generate a new DebitCard class instance with the account
    //object created earlier
    DebitCard debitCard = new DebitCard(Account account);
    should be changed to this
    Java Code:
    //Generate a new DebitCard class instance with the account
    //object created earlier
    DebitCard debitCard = new DebitCard(account);

  6. #6
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    k thanks everyone I will try this when I get home. One more newbie question while I'm here. Why does the DebitCard(Account account) use the same name as the class. What I mean is this method uses a Capitol D and a Capitol C. I thought only constructors could use the exact name as a class. Wouldn't the method need a lower class d like this.
    debitCard(Account account)

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    We practice on newbies because it is the newbie mistakes that bring down masters.

    The answer to you question is that AAAA is different from aaaa iow upper and lower casing are distinct for java namespaces.

    Thank you for letting us practice on you, that's how we stay afloat.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    you can practice on me all you want! I understand that AAA is differet than aaa but why do the methods in this class use the exact same as the class. I thought only constructors could use the exact same name as the class. Therefor if the class is name DebitCard and constructor is name DebitCard then doesn't the method need to begin with a lowercase d like debitCard? Instead it is name DebitCard exactly the same as the class. Isn't this bad practice?

  9. #9
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    None of this is working,

    When I use this code
    new DebitCard(account)
    I get a red sqwiggle under DebitCard that says
    cannot find symbol
    symbol: constructor DebitCard(coldbeveragejava.AccountRepository
    lcoation class: coldbeveragejava.DebitCard

    When I use this code i get a red sqwiggle under the whole line that says

    ')' expected
    ';' expected
    inconvertable types
    found:coldbeveragejava.AccountRepository
    required: coldbeverage Account


    When I use this code
    DebitCard debitCard = new DebitCard(account);
    I get a red sqwiggle under DebitCard that says
    cannot find symbol
    symbol: constructor DebitCard(coldbeveragejava.AccountRepository)
    location: class coldbeveragejava.DebitCard

    any suggestions?

  10. #10
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    The second code I used didnt post so here it is


    new DebitCard((Account account = new Account));

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by CirKuT View Post
    new DebitCard((Account account = new Account));
    Nit: there is no difference between
    Java Code:
    new DebitCard((Account account = new Account));
    Java Code:
    new DebitCard(new Account);
    As you lose access to "account" as soon as the contructor returns.

    Most folks would write it as

    Java Code:
    Account account = new Account);
    DebitCard aDC = new DebitCard((account);

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    Have you tried using the javac command directly vs whatever you are using to get the errors?
    Copy and paste javac's error messages here (without any edits).

  13. #13
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Im sorry, Ive only been doing Java for 5 weeks now, I only know how to use Netbeans :-( I wouldnt need to come here but the professor of my class sucks and wont answer anyones email

  14. #14
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    In response to fishtoprecords,

    Your second line of code gives me no red sqwigglys but the first line gives the error,

    '(' or '[' expected

    Local variable hides a field

  15. #15
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    it should look like this

    Java Code:
    Account account = new Account();
    DebitCard debitCard= new DebitCard(account);
    if you are getting a red line under DebitCard with a "DebitCard" cannot be resolved issue, your compiler cannot find your DebitCard class.
    i.e. you are missing the

    Java Code:
    import com.mypackagename.DebitCard
    statement from your code

    Is it in the same package as the class you are calling it from? See if your IDE has a feature that will resolve the dependencies by importing the necessary classes for you.

  16. #16
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by CirKuT View Post
    Your second line of code gives me no red sqwigglys but the first line gives the error,
    '(' or '[' expected
    So add a ( or two until it works.
    I'm not going to give you perfect code, you need to learn and understand.

  17. #17
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Oh i gotcha I thought you gave me code you thought would work, I get what you are trying to do and I appreciate it, Im not trying to make you pro's do my work, Id like to be at your level one day also.
    Thanks alot, ill give this a shot

  18. #18
    CirKuT is offline Member
    Join Date
    Sep 2008
    Posts
    53
    Rep Power
    0

    Default

    Fishtoprecords,

    I tried using this
    Account account =new Account()
    I assume that is where the extra ( went but I am still getting errors. Is this what I was supposed to do?

  19. #19
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by CirKuT View Post
    Fishtoprecords,

    I tried using this
    Account account =new Account()
    I assume that is where the extra ( went but I am still getting errors. Is this what I was supposed to do?
    What's the error?
    How did you try to fix it?

  20. #20
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation careful, clyde ....

    Quote Originally Posted by CirKuT View Post
    the ......... of my ..... ducks
    You gotta be careful working at this caliber of respondent, I posted to a blog on the anit-polar geographic of my physical location and a bunny rabbit there with twenty eight thousand near flawless posts running often 10-k of text knew my preferred casual name. All three or so of these people here are supermasters. You said we could practice on you, so further - after an elaborately build barrier wall, a nationally known group placed a physical body in front of me with software I neither need nor want. Another group flooded my paper mail channel when I hired a Tier-1 server with impeccable security practices. What I did to fix the problem is well beyond anyones grasp.

    I would like to know what effect that has on you, but in any event realize there are a great number of people who can read what you say and have easy tools to identifiy you.

    Quote Originally Posted by CirKuT View Post
    Im sorry, Ive only been doing Java for 5 weeks now,...
    These are common questions at this level, we feed on them to wake up before working on our own work.
    Quote Originally Posted by CirKuT View Post
    I wouldnt need to come here but .... wont answer anyones email
    electronic traffic, except when protected by advanced work, is not worth answering anyway ... don't worry about it. I bet you get tons of useless junk in your email.

    I don't.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Page 1 of 2 12 LastLast

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 04:01 PM
  2. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 12:25 AM
  3. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 01:05 PM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM
  5. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 06:24 PM

Posting Permissions

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