Results 1 to 20 of 20
Thread: Newbie search array question
- 09-13-2008, 06:25 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 06:35 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 09-13-2008, 06:54 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 06:58 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 08:08 AM #5
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
- 09-13-2008, 08:27 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 08:29 AM #7
change the code, Use the same words, exactly in both places.
report back
- 09-13-2008, 08:50 AM #8
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 08:54 AM #9
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
- 09-13-2008, 09:04 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 09:08 AM #11
What do you expect your code, here, to do?
Its got bugs, and I'm not going to point out what they are. This is homework, you have to do the homework to learnJava Code:public coldbeveragejava.Account getAcount(int accountID) { if (accountID == accounts); return accounts[accountID]; }
- 09-13-2008, 09:08 AM #12
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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!
- 09-13-2008, 09:14 AM #13
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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.
- 09-13-2008, 09:16 AM #14
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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.
- 09-13-2008, 09:19 AM #15
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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?
- 09-13-2008, 06:38 PM #16
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:
and getting 171 to load.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.
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
- 09-13-2008, 09:19 PM #17
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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
- 09-13-2008, 10:51 PM #18
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 beJava 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
- 09-14-2008, 06:13 AM #19
Member
- Join Date
- Sep 2008
- Posts
- 53
- Rep Power
- 0
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.
- 09-14-2008, 06:26 AM #20
Similar Threads
-
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM -
Newbie question; Vectors
By Kern in forum New To JavaReplies: 7Last Post: 08-03-2008, 06:59 AM -
Problem with displaying search results from an array
By BHCluster in forum New To JavaReplies: 4Last Post: 04-24-2008, 03:34 AM -
Array Search Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM -
Newbie question about Static methods
By SCS17 in forum New To JavaReplies: 9Last Post: 02-06-2008, 08:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks