aspose file tools*
The moose likes Beginning Java and the fly likes Finding and changing array elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding and changing array elements" Watch "Finding and changing array elements" New topic
Author

Finding and changing array elements

RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
HELP! Need to know if there is a better way to find,based on a three-digit member id (memId) a record of a first name, last name....etc and alter one aspect (be it first name, membership fee...etc) of it. Im having trouble finding the code that will match up the answer with the name. The problem area is towards the end, line 214 to 247.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As I recall, the information in all of these arrays correspond by index. For example, the person with member ID memId[17] has a first name of fname[17], and an age of age[17], and so on, right?

So I think you're asking how to find the index of an element in an array. That is, you have an int array called memId, and you're given a 3-digit number, and you want to find the index in memId that holds that number. Once you have this index, you will know the position to edit in all the other arrays.

The order of elements is critical in your arrays, so you cannot use Arrays.binarySearch (because this search requires a sorted array). So basically, you need to iterate through the array until you've found a match.

I suggest writing your own search method that returns the index of a certain member ID. The method would just iterate through the memId array, looking for a match of the input ID number. As soon as it finds a match, it would return the index number. If no match is found, it should return some special value (like -1) to indicate no match.

Does that help?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
Boy your'e in trouble when someone on the board recognizes the monstrosity you've been struggling with.

I have several arrays, first name (fname) last name (lname).....and so on. I can code a menu with the best of them. I can even parse it into an integer. My problem is, if i ask a user (enter memberid) and they enter a 3 digit number, ill ask them what do they want to change (again...first name...last name) and I pretty much know the END of that story....something like

But ive tried just about everything to get fname[i] to match the 3-digit memId the user enters. Thats all the help i need. Once i have that....its off to the races. And i can handle it if its a double or an int (which i will be dealing with with other arrays).
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

fname holds Strings of names, so its elements are not going to match the id, which is an int.

I think you just want to find the index of the id in memId, and then use that index to know which String to modify in fname. For example, if you write your own method called "search"...
RJ Cavender
Ranch Hand

Joined: Jul 26, 2006
Posts: 51
THank you for your help. If i make it through this, ill try and work hard to return to help out. "IF"...i make it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding and changing array elements
 
Similar Threads
BufferedReader help
Logic behind deleting
ArrayIndexOutOfBounds
Incompatible types
Passing count