Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "Student Not Found" Problem

    I'm trying to display a message that informs the user that the name he entered was not found in the list of students. I wrote a binary search to go through the list of names to compare to the search key.

    Code:
    		while(repeat)
    		{
    			searchEntry = JOptionPane.showInputDialog("Enter the last name of the student you wish to see the grade of: ");
    			if(searchEntry.length() == 0)
    			{
    				System.exit(0);
    			}
    			int low = 0;
    			int high = studentArray.length - 1;
    		
    			while(high >= low)
    			{
    				int mid = (low+high)/2;
    				if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) < 0)
    				{
    					high = mid - 1;
    
    				}
    				else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) > 0)
    				{
    					low = mid + 1;
    				}
    				else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) == 0)
    				{
    					JOptionPane.showMessageDialog(null, studentArray[mid].firstName + " " + studentArray[mid].getLastName() 
    														+ " received a grade of " + studentArray[mid].getNumberGrade());
    					high = -1;
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(null, "Name not found.");
    				}
    			}
    		}
    Here, I'm using an if-else-if statement, but it is not working the way I want it to.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The first question to ask - has studentArray been sorted prior to this point?
    I'll take a look at this when I get home as well.

  • #3
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, the array of objects called studentArray has already been sorted in alphabetical order according to last name.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Looks alright to me; you'll never get to the else condition though. The search entry will always be <, = or > the current iteration, regardless of if it exists in the list.
    If you make it through the list without a result, then you have no entry found.

    Code:
    			int low = 0;
    			int high = studentArray.length - 1;
    		
    			Student result = null;
    			while(high >= low)
    			{
    				int mid = (low+high)/2;
    				if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) < 0)
    				{
    					high = mid - 1;
    
    				}
    				else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) > 0)
    				{
    					low = mid + 1;
    				}
    				else if(searchEntry.compareToIgnoreCase(studentArray[mid].getLastName()) == 0)
    				{
    					result = studentArray[mid];
    					high = -1;
    				}
    			}
    			if (result != null)
    			{
    				System.out.println("Student found: " + result);
    			}
    			else
    			{
    				System.out.println("No student with last name: " + searchEntry);
    			}
    The algorithm is otherwise correct.

  • #5
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So how exactly would I make a message display only if no match is found? Delete the else statement, and then make an if outside of the while loop? But what could I make as the condition of this if statement?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The if would just become if (result == null) to indicate that no match is found.

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts
    you sir, are awesome. Thank you very much.


  •  

    Posting Permissions

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