Question

I have completed most of the code by myself (with the help of a bit of Googling) but I have run into an unexpected problem. First-off, I have to sort a user entered list of names in aplhabetical order of their last names using selection sort. Here is my code:

    import java.util.*;
class Name_Sort
{
    public static void main (String args[])
    {
        Scanner in = new Scanner (System.in);
        System.out.print ("Enter the number of names you wish to enter: ");
        int n = in.nextInt();
        String ar[] = new String [n];
        for (int i = 0; i<ar.length; i++)
        {
            System.out.print("Please enter the name: ");
            ar[i]= in.nextLine();
        }
        String temp;
        for (int b = 0; b<n; b++)
        {
            for (int j=b+1; j<n; j++)
            {
                if ((compareLastNames(ar[b], ar[j]))>0)
                {
                    temp = ar[b];
                    ar[b] = ar[j];
                    ar[j] = temp;
                }
            }
        }
        System.out.println ("The names sorted in alphabetical order are: ");
        for (int a = 0; a<n; a++)
            System.out.print (ar[a]+"\t");
    }

    private static int compareLastNames(String a, String b) 
    {
        int index_a = a.lastIndexOf(" ");
        String surname_a = a.substring(index_a);
        int index_b = b.lastIndexOf(" ");
        String surname_b = b.substring(index_b);
        int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
        return lastNameCmp;
    }
}

The problem (I think) is arising when I'm taking the names from the user, specifically, this part:

Scanner in = new Scanner (System.in);
    System.out.print ("Enter the number of names you wish to enter: ");
    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.
    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }

The output on the terminal window of BlueJ shows up as

Name_Sort.main({ });
Enter the number of names you wish to enter: 5
Please enter the name: 
Please enter the name: 

That is not what it's supposed to display. What could I be doing wrong? I've pondered over it for a while, but nothing comes to mind.

And, even if I do move forward and enter a few names despite the error above, I get another error in this part of my code here:

private static int compareLastNames(String a, String b) 
{
    int index_a = a.lastIndexOf(" ");
    String surname_a = a.substring(index_a);// This is the line the compiler highlights.
    int index_b = b.lastIndexOf(" ");
    String surname_b = b.substring(index_b);
    int lastNameCmp = surname_a.compareToIgnoreCase(surname_b);
    return lastNameCmp;
}

the error is :

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (injava.lang.String)

Does this mean that the white-space character " " is not present? But why?

This is a screenshot of the terminal window: http://imgur.com/l7yf7Xn

The thing is, if I just initialize the array with the names first (and not take any input from the user) the codes runs fine and produces the desired result. Any help please?

Also, since I know some people here are very particular about this, yes, this is a homework assignment, yes, I did do all of the code by myself, I googled on how to sort the names in alphabetical order as I couldn't exactly code out the original idea I had. Which was comparing the ASCII values of each character of two surnames to see which should come first. Like: if((int) surname1.charAt(0)>(int) surname2.charAt(0)) then surname2 should come before surname1, else if they both have the same first character, take the second character and so on.

Thanks for taking the time to read this.

Was it helpful?

Solution

The problem is with the in.nextInt() command it only reads the int value. So when you continue reading with in.nextLine() you receive the "\n" Enter key. So to get around this you will have to add an extra in.nextLine() before going into the loop. Or, use another scanner.

    int n = in.nextInt();
    String ar[] = new String [n]; //Array to store the names in.

    in.nextLine(); // < --- an extra next Line

    for (int i = 0; i<ar.length; i++)
    {
        System.out.println("Please enter the name: ");
        ar[i]= in.nextLine();

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top