CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   "Student Not Found" Problem (http://www.codingforums.com/showthread.php?t=254312)

trantommyd 03-16-2012 09:39 PM

"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.

Fou-Lu 03-16-2012 10:00 PM

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.

trantommyd 03-18-2012 05:12 AM

Yes, the array of objects called studentArray has already been sorted in alphabetical order according to last name.

Fou-Lu 03-18-2012 06:52 PM

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.

trantommyd 03-18-2012 08:18 PM

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?

Fou-Lu 03-18-2012 08:25 PM

The if would just become if (result == null) to indicate that no match is found.

trantommyd 03-18-2012 09:24 PM

you sir, are awesome. Thank you very much.


All times are GMT +1. The time now is 01:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.