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

    Default Newbie search array question

    Hello everyone,
    Im still new to Java so this should be an easy question to answer but I am stuck. Any suggestions would be great!

    I have two classes; KeypadDisplay and AccountRepository.

    The KeyPadDisplay class has the following two methods in regards to the problem;

    public void askUserToInsertDebitCard() {

    //creates variable to hold user input
    int ID;

    //creates new scanner object with the title accountID
    Scanner accountID = 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)");
    ID = accountID.nextInt();

    AccountRepository acount = AccountRepository.getInstance();
    acount.getAcount(ID);

    and the second method is

    public void displayMessage(String msg) {
    System.out.println(msg);
    }

    Ok so as you can see I am calling the AccountRepository.getAcount() method and using ID as the parameter. What I need to do is check the accountID the user enters with the array found in the AccountRepository class.

    Here is the array from the AccountRepository class and the method I am calling.

    private AccountRepository() {
    int accountValue = 100;
    int accountID=1;
    for(int i=0; i< MAX_ACCOUNTS; i++){
    accounts[i] = new Account(accountID++, accountValue+=100);
    }
    }

    private static Account[] accounts = new Account[MAX_ACCOUNTS];

    public coldbeveragejava.Account getAcount(int accountID) {
    return null;
    }

    What I cannot figure out is what to add to the getacount method to get it to check the ID with the ID's found in the array and then returning the value back to the askUserToInsertDebitCardClass.

    If my question does not make sense here are the exact instructions

    "After the user gives the account ID to askUserToInsertDebitCard(), use the AccountRepository getAcount method to look up the account by ID and return an account object to askUserToInsertDebitCard(). Note, getAccount must match the account ID that it gets as a parameter against the stored accounts and return the first matching account."

    Thanks to anyone who takes the time to read this,

    Morgan

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    In simple word, what you want to find is the account object belongs to the given account ID, is that right?

    If so you have to return an object, that's it.

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

    Default

    Thanks for the reply,

    Basically the last paragraph explains the exact problem. A user will enter either a 1 or a 2 as an accountID and then I need to search the array (accountID 1 and accountID 2 was already added to the array with a different method) and return the first matching record.

    This is what Ive tried

    private AccountRepository() {
    int accountValue = 100;
    int accountID=1;
    for(int i=0; i< MAX_ACCOUNTS; i++){
    accounts[i] = new Account(accountID++, accountValue+=100);
    }
    }

    private static Account[] accounts = new Account[MAX_ACCOUNTS];

    public coldbeveragejava.Account getAcount(int accountID) {
    System.out.println("Entering AccountRepository getAcount");
    return accounts[accountID];
    }//This does not work at runtime

    and also this

    private AccountRepository() {
    int accountValue = 100;
    int accountID=1;
    for(int i=0; i< MAX_ACCOUNTS; i++){
    accounts[i] = new Account(accountID++, accountValue+=100);
    }
    }

    private static Account[] accounts = new Account[MAX_ACCOUNTS];

    public coldbeveragejava.Account getAcount(int accountID) {
    if (accountID == accounts);
    return accounts[accountID];
    }//This gives me a message stating incomparable types

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

    Default

    If it helps anyone I can upload the whole package with all the classes.

    The instructions have been added to the code as comments for ease of
    access.

    Morgan

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

    Default

    Quote Originally Posted by CirKuT View Post
    Java Code:
        
        private static Account[] accounts = new Account[MAX_ACCOUNTS];
        
        public coldbeveragejava.Account getAcount(int accountID)  { 
            System.out.println("Entering AccountRepository getAcount");
            return accounts[accountID];
        }
        private static Account[] accounts = new Account[MAX_ACCOUNTS];
        
        public coldbeveragejava.Account getAcount(int accountID)  { 
            if (accountID == accounts);
            return accounts[accountID];
        }//This gives me a message stating incomparable types
    Why are you using different definitions for "Account" in your code?
    You have
    private static Account[] accounts ...
    and
    public coldbeveragejava.Account getAcount(...

    Why "coldbeveragejava.Account" in one place and "Account" in another? Use the same words, exactly in both places

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

    Default

    To be honest, This is a school assignment, I did not write the basic code I am just adding to the original design, I was wondering this myself but I am to new to java to understand why a programmer would do this. I have this last problem that I cannot figure out and I am finished.

    Im going to upload the package as a zip file for anyone interested in checking out my problem. To see this instructions on what I need to do simply open the KeyPadDisplay class and scroll down to the public void askUserToInsertDebitCard() method. The instructions are right above it in a bunch of comment lines.

    Thanks to anyone who can help me or takes the time to reply
    Attached Files Attached Files

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

    Default

    change the code, Use the same words, exactly in both places.
    report back

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

    Default

    The whole project has 10 classes, i only reffered to the ones I am editing. There is an Account class also maybe thats why they used coldbeveragejava.Account. I noticed when I change the getAcount method to Return MAX_ACCOUNTS I get an error that says it requires coldbeveragejava.Account, maybe its an inheritance thing? I thought that required the use of "extend" though? Again Im still new to this.

    So what do I change the return null; area of the getAcount to? thats where Im having the problem. Im supposed to change this area so that it checks the accountID the user entered against the array of other account IDs and return the first matching account. How do I write this syntax?

    Thanks

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

    Default

    You have to return a real object, the one you find in the search.
    But you *must* declare and return the same kind of object.

    Try declaring everything as coldbeveragejava.Account

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

    Default

    I know I have to return a real object but I dont know how to tell java what search and how to search it. I know that the syntax "return null;" will return no value but anything further and Im stumped. I will try declaring everything as coldbeveragejava.Account

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

    Default

    What do you expect your code, here, to do?

    Java Code:
      public coldbeveragejava.Account getAcount(int accountID)  { 
            if (accountID == accounts);
            return accounts[accountID];
        }
    Its got bugs, and I'm not going to point out what they are. This is homework, you have to do the homework to learn

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

    Default

    fishtoprecords,

    I tried declaring some things as coldbeveragejava.Account and NetBeans started yelling at me hehe. I got some errors. I dont know why the designers added that in. Im stumped though in how to search this array and return the value.
    Anyways feel free to look at the attachment I uploaded if your bored but Im off to bed. Ill start working on it again tomorrow.
    Thanks for relplying!

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

    Default

    Oh that code does not do anything thats why I added the comment under it saying it gave back an error. I was just trying things.
    I wouldnt expect you too just give me the answer, I want to learn Java and for only doing it for three weeks I feel we are moving a little fast. We are all ready working on inheritance and polymorphism. I think thats a little fast for three weeks. My problem is that it is an online class and the professor is barely available for chats leaving me to learn from examples on the Internet.

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

    Default

    Yes there are bugs, The AccountRepository class has not been touched yet. Basically this is exactly how the school sent it to me and then they give me instructions within comment lines to explain what to do but never really tell me how or why its doing what its doing.

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

    Default

    So since I need to return a real object can I use one that was already created within the class or so I need to create a new constructor within the getAcount method?

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

    Post

    Beverage[][] should be a list of Pairs. int[] prices should be a member variable of class whateveritisyouareselling. main(String[] args) is in class machine so we don't need a Main class, and if this is a student project using static factory methods to get a AccountRepository, basically what you need to do is focus on the comment in the code:
    Java Code:
          //getAccount must match the account ID that it gets as a parameter
          //against the stored accounts and return the first matching account. 
          //This is code that you must add to the starter code.
    and getting 171 to load.

    Probably use the Account that is already constructed, usually one does not new an account at this study level - doing so would be some pretty sophisticated security reason of some kind and is not likely what your instructor wants to achieve right now. It looks like sorting and searching are the objectives today.

    Is this college work or advanced placement?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    This is an introduction to Java. I have 4 Java classes, and we keep building and changing the starter code hence the reason its not to sophisticated yet.
    As for using the account already created, I know I need to take the accountID that was entered by the user and search the Account array to see if the ID exists and then return it. I understand what I need to do but I dont know how to write te syntax. Ive tried if statements and eveything.
    Anytime I use the Account object in the getAcount method it says I cannot use it there, I get the red squiggly line uder it in NetBeans.

    Any suggestions? My school expects me to learn how to search and return array values in three days and all my research has got me nowhere. Ive finished eveything else for this assignment but this last task is killing me!

    Thanks everyone!

    Morgan

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

    Default squiggles are wiggles

    Hold the mouse over the red tiggle, and tell us 'exactly' what it say, spell and word exactly with no additives or explainations.

    Situations like this is why I walked from college, what we probably have here is a buch of Beans. There is too much to get done in the time allotted.

    What the code shows, given your question, would be
    Java Code:
    int length = accouts.length;
    do
    {
      if(accouts[--length] == id numer)
      {  
          processRequest();
          return;
      }
    }
    while(length > 0x0000);
    // if we get here, account was not found....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

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

    Default

    OK I will try your code,

    I do think I go tit working with this, Im not 100% sure but I am getting no errors let me know what you think,

    In the begining the getAcount method looked like this,

    public coldbeveragejava.Account getAcount(int accountID) {
    return null;
    }

    I needed to change it so it took the accountID and checked it against the accounts that already existed, Note that a user can only enter a 1 or a 2 and both these accountID's already exist in the array,
    Here is what I modified the code to

    public coldbeveragejava.Account getAcount(int accountID) {

    // loop through accounts searching for matching account number
    for ( Account currentAccountID : accounts )
    {
    // return current account if match found
    if ( currentAccountID.getAccountNumber() == accountID )
    return currentAccountID;
    }
    return null;
    }

    Its getting no errors and it seems to work.

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

    Default

    Quote Originally Posted by CirKuT View Post
    Its getting no errors and it seems to work.
    If it has no errors, and works, then its time for a beer

Similar Threads

  1. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  2. Newbie question; Vectors
    By Kern in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 06:59 AM
  3. Problem with displaying search results from an array
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 03:34 AM
  4. Array Search Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 PM
  5. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM

Posting Permissions

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