Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding and changing array elements

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic