Results 1 to 8 of 8
Thread: wont display selected array
- 03-21-2011, 12:36 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
wont display selected array
When im searching for a particle name details in an array it keeps displaying the all details in it even if the name is not in the array....please help
Java Code:private static void searchAthleteName(athlete[]ath, int size) { String athSurname = null; String athForename = null; My.p("\n\t\t\tS E A R C H B Y N A M E "); My.p("\n\n\tEnter Athlete Surname : "); athSurname = key.nextLine(); My.p("\n\tEnter Athlete Forename : "); athForename = key.nextLine(); My.p("\nAthlete No Surname Forename Date of Birth Best Time"); for(int x = 0; x < size; x++) { if (athSurname.equals(athSurname) && athForename.equals(athForename)) { My.p(ath[x].toString()); } else { My.p("\n\t\tNot a Valid Name "); } } My.pressKey(2); }
- 03-21-2011, 12:47 AM #2
It is impossible for this statement to ever be false. Tell when athSurname will not be equal to athSurname. Perhaps you should be comparing to a value in the array instead.Java Code:if (athSurname.equals(athSurname) && athForename.equals(athForename))
- 03-21-2011, 12:54 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
not sure what you mean?
im pretty new to programming sorry
- 03-21-2011, 12:58 AM #4
Oh come on. In the if statement you are comparing a variable to itself. It has to be true. It cannot be false.
- 03-21-2011, 01:04 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 94
- Rep Power
- 0
The above code will always be true. For some reason, you are checking to see if the athSurname object is equal to itself. Not sure why you would do that.Java Code:athSurname.equals(athSurname)
-
try something like this instead:
Java Code:for (String s:ath) { if (s == athSurname || s == athForename) { //name matched } }
as everyone else is saying... you should search for the name in your athlete array 'ath' rather than search for the athlete name in your variable which holds the search term 'athSurname'
- 03-21-2011, 01:06 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
like i said im new to programming
i thought when i compare the athSurname.equals(athSurname) && athForename.equals(athForename) it will only display if the surname and forename match what id asked/
-
the thing you need to understand is that when you declare a variable, e.g.
Java Code:athSurname = key.nextLine();
say the user typed "Doe" as the surname, so when you write what you did:
Java Code:athSurname.equals(athSurname)
you are asking the java virtual machine to find out if "Doe" equals "Doe", which it always will. you need to ask the computer if you have an athlete called "Doe" in your Athlete array instead, and if yes, then load the rest of "Doe"'s records
Similar Threads
-
I want to know how to calculate and display the sum of each column in 2D array
By Javanoobs in forum New To JavaReplies: 1Last Post: 03-03-2011, 06:43 AM -
how to display image selected by JFileChooser
By khushi.cutegal in forum AWT / SwingReplies: 17Last Post: 07-24-2010, 06:03 AM -
Trying to display the length of the array using tags
By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-03-2010, 12:15 AM -
uploaded image wont display if i change filename
By schenker in forum Java ServletReplies: 12Last Post: 06-11-2010, 11:13 AM -
[SOLVED] Last line in JTextArea wont display
By Chris.Brown.SPE in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 01:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks