Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2012, 09:39 PM   PM User | #1
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
"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.
trantommyd is offline   Reply With Quote
Old 03-16-2012, 10:00 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 03-18-2012, 05:12 AM   PM User | #3
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
Yes, the array of objects called studentArray has already been sorted in alphabetical order according to last name.
trantommyd is offline   Reply With Quote
Old 03-18-2012, 06:52 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 03-18-2012, 08:18 PM   PM User | #5
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
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?
trantommyd is offline   Reply With Quote
Old 03-18-2012, 08:25 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The if would just become if (result == null) to indicate that no match is found.
Fou-Lu is offline   Reply With Quote
Old 03-18-2012, 09:24 PM   PM User | #7
trantommyd
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
trantommyd is an unknown quantity at this point
you sir, are awesome. Thank you very much.
trantommyd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 PM.


Advertisement
Log in to turn off these ads.