Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-13-2008, 08:25 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-13-2008, 08:35 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-13-2008, 08:54 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-13-2008, 08:58 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-13-2008, 10:08 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
Quote:
Originally Posted by CirKuT View Post
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 09-13-2008, 10:27 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
File Type: zip coldbeveragejava.zip (31.8 KB, 1 views)
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 09-13-2008, 10:29 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
change the code, Use the same words, exactly in both places.
report back
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 09-13-2008, 10:50 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 09-13-2008, 10:54 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 09-13-2008, 11:04 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 09-13-2008, 11:08 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
What do you expect your code, here, to do?

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
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 09-13-2008, 11:08 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 09-13-2008, 11:14 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 09-13-2008, 11:16 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 09-13-2008, 11:19 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 09-13-2008, 08:38 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 783
Nicholas Jordan is on a distinguished road
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:
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?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 09-13-2008, 11:19 PM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 09-14-2008, 12:51 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 783
Nicholas Jordan is on a distinguished road
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
Code:
int length = accouts.length; do { if(accouts[--length] == id numer) { processRequest(); return; } } while(length > 0x0000); // if we get here, account was not found....
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 09-14-2008, 08:13 AM
Member
 
Join Date: Sep 2008
Posts: 37
CirKuT is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 09-14-2008, 08:26 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 475
fishtoprecords is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
make search function ike eclipse search in window->preference i4ba1 Advanced Java 5 08-26-2008 05:43 PM
Newbie question; Vectors Kern New To Java 7 08-03-2008 08:59 AM
Problem with displaying search results from an array BHCluster New To Java 4 04-24-2008 05:34 AM
Array Search Test Java Tip java.lang 0 04-14-2008 10:45 PM
Newbie question about Static methods SCS17 New To Java 9 02-06-2008 10:03 AM


All times are GMT +3. The time now is 11:07 AM.