Results 1 to 8 of 8
  1. #1
    newbie80 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Exclamation 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);
    	}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    if (athSurname.equals(athSurname) && athForename.equals(athForename))
    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.

  3. #3
    newbie80 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    not sure what you mean?
    im pretty new to programming sorry

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Oh come on. In the if statement you are comparing a variable to itself. It has to be true. It cannot be false.

  5. #5
    Join Date
    Mar 2011
    Posts
    94
    Rep Power
    0

    Default

    Java Code:
    athSurname.equals(athSurname)
    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.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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'

  7. #7
    newbie80 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    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/

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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

  1. Replies: 1
    Last Post: 03-03-2011, 06:43 AM
  2. how to display image selected by JFileChooser
    By khushi.cutegal in forum AWT / Swing
    Replies: 17
    Last Post: 07-24-2010, 06:03 AM
  3. Trying to display the length of the array using tags
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-03-2010, 12:15 AM
  4. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 11:13 AM
  5. [SOLVED] Last line in JTextArea wont display
    By Chris.Brown.SPE in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 01:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •