• Post Reply Bookmark Topic Watch Topic
  • New Topic

Search & Display Info from Parallel Array  RSS feed

 
Steve Quill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, just another newbie here needing help with a coding dilemma. The program I'm trying to create is a small database that has to use three parallel arrays (userNumber, player, and highScore). The intention is that the user should enter the a user number at the prompt and the program will display the person's information. If the number entered doesn't exist then the program should make them re-enter a user number until a correct one is entered.
The problem I'm running into now is that the program doesn't accept any user number except the first user number, whether or not it's a valid number in the array list. Could someone tell me what I'm doing wrong here?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,
Let's look at what your code does. The for loop starts with i as 0. The while loop checks if you've entered that first user. If you haven't, it prompts you again and keeps prompting. You never get back out to the while loop.

 
Steve Quill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jeanne,
To simplify the code I did remove the "For" section, but I'm at a loss on how to proceed from there because the code won't seem to work at all unless I include the "int i=0;" portion. When I've tried to remove it then I can't seem to figure out how to get the program to display the appropriate array information for just the user selected.

In short, I guess the right question would be: Exactly what should I change in order for the program to return the info for just the array entries that correspond to the entered userNumber?

Here's the new/slightly-modified code:
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about this: after the user number is entered, how do you know if it's valid or not?

A good thing to do in this situation is to write done in plain English exactly what the program should do -- as if you were talking to a 10-year-old.  Don't use any Java terms.
 
Steve Quill
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Knute,
I reviewed the code again last night and this morning (two almost-full days on this now) but unfortunately I still don't know what to enter to have the entry check all userNumbers and return all applicable array elements. Because this is for an assignment with specific instructions, I'm kinda stuck on how to code this. The code I posted before was more basic than what I actually need to end up with, which I thought may help me get more responses, but what I'll post below is much closer to what I need to end up with.

In plain English, the program must be in dialog boxes and contain the information in five parallel arrays with 20-50 elements each. Once I can figure out how to get this to work with three arrays with 5 elements each, then I'll add the rest of what I need to the program.

The final output, all in dialog boxes, needs to essentially be:

- Initial prompt: "Have user enter a user number"
- Correct userNumber entered: "Message with user number, name, and score"
- Incorrect userNumber entered: "Error message with number just entered" & "Loop to have user try again until correct number is entered"

This code is much closer to what I need to end up with:

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of this

I tend to have a helper method to do the searching, something like:

Then your while loop could utilize that.

Then your 'index' variable can be used to access data in your other arrays.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The format string has some errors. Here are the basic formats:
%s - display a string
%d - display an int (or long)
%f - display a double (or float)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!