简体   繁体   中英

Sorting array of names alphabetically using compareTo()

I am trying to sort an array of names alphabetically by using compareTo() and a method String addSort(String name) but I get an error when compiling at the line "return name", saying that my "variable "name" may not be initialized" when it already is.

I've made the method and the code for sorting alphabetically which I think should be correct when using compareTo() as a way of sorting the array. (The array "moreFriends" is a new array which doubles the size of the original array "friends" when it gets full) (Note this is not all of the code)

public class SimpleDataStructure{

    private String [] friends;
    private String [] moreFriends;
    private int counter;

    public SimpleDataStructure()
    {
        friends= new String[5];
        counter=0;
    }
public String addSort(){
        String name;
        for(int i = 0; i < moreFriends.length; i++){

            for(int j = i + 1; j < moreFriends.length; j++){

                if(moreFriends[i].compareTo(moreFriends[j]) > 0){
                    String temp = moreFriends[i];
                    moreFriends[i] = moreFriends[j];
                    moreFriends[j] = temp;
                }
            }
        }
        System.out.println("Names in sorted order:");
        for(int i = 0; i < moreFriends.length -1; i++){
            System.out.println(moreFriends[i]);   
        }
        return name;   
    }
public static void main( String [] arg){
SimpleDataStructure sortedfriends = new SimpleDataStructure();
        System.out.println(sortedfriends.addSort(name));
}

This is the error message I get when i try to compile the program:

SimpleDataStructure.java:85: error: variable name might not have been initialized
        return name;
               ^
1 error

When I expect the output to eventually be:

(unsorted)
Kalle
Bob
Carl
Alice
Lewis

(sorted) 
Alice
Bob
Carl
Kalle
Lewis

You need declare youre function like this:

public String addSort(String name){

and delete the string declaration:

String name;

and you don't put value for name.

You can solved your problem using this:

String [] a="a","v","b";
Arrays.sort(a);

The reason that you are getting the compile error is because you never set a value to the String name before trying to use it.

You should be passing in the value like you have in your description addSort(String name) . This will remove that error.

I do not see a reason why you are returning the String in your function. This function does not appear to add the passed in name either.

Hope this helps!

import java.util.*;


class SimpleDataStructure {

public String[] addSort(String moreFriends []) {

    for (int i = 0; i < moreFriends.length; i++) {

        for (int j = i + 1; j < moreFriends.length; j++) {

            if (moreFriends[i].compareTo(moreFriends[j]) > 0) {
                String temp = moreFriends[i];
                moreFriends[i] = moreFriends[j];
                moreFriends[j] = temp;
            }
        }
    }


    return moreFriends;
}

public static void main(String[] arg) {
    Scanner input=new Scanner(System.in);

    SimpleDataStructure sortedFriends = new SimpleDataStructure();

    String [] name =new String[5];
    System.out.println("Enter name(s): ");
    for (int i = 0; i < name.length; i++) {
        name[i]=input.nextLine();
    }

    System.out.println("Unsorted:");
    System.out.println(Arrays.toString(name));

    System.out.println("Sorted:");
    System.out.println(Arrays.toString(sortedFriends.addSort(name)));
}

}

And if you want the names to print out line by line, just create a for loop instead of Arrays.toString

Or you could even use Arrays.sort , which is much simpler

import java.util.Arrays;

class SimpleDataStructure {

public String[] addSort(String moreFriends []) {

    for (int i = 0; i < moreFriends.length; i++)
        Arrays.sort(moreFriends);
    return moreFriends;
}

public static void main(String[] arg) {

    SimpleDataStructure sortedFriends = new SimpleDataStructure();
    String [] name ={"Kalle", "Bob","Carl","Alice", "Lewis"};


    System.out.println("Unsorted:");
    System.out.println(Arrays.toString(name));

    System.out.println("Sorted:");
    System.out.println(Arrays.toString(sortedFriends.addSort(name)));
}

}

I changed my arrays "friends" and "moreFriends" to static in my class. Now my code looks something like this when im calling my method in my main:

SimpleDataStructure sorted = new SimpleDataStructure();
        System.out.println("Sorted:");
        System.out.println(Arrays.toString(sorted.addSort()));

And this is my method:

public String[] addSort() {

            for (int i = 0; i < moreFriends.length; i++) {

            for (int j = i + 1; j < moreFriends.length; j++) {

                if (moreFriends[i].compareTo(moreFriends[j]) > 0) {
                    String temp = moreFriends[i];
                    moreFriends[i] = moreFriends[j];
                    moreFriends[j] = temp;
                }
            }
        }
        return moreFriends;
}

However i get this error message now:

Unsorted:
Kalle Bob Carl Alice Lewis
Sorted:
Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.compareTo(Unknown Source)
        at SimpleDataStructure.addSort(SimpleDataStructure.java:75)
        at SimpleDataStructure.main(SimpleDataStructure.java:108)

You need to include your variable name, that holds the names, inside addSort

SimpleDataStructure sorted = new SimpleDataStructure();
    System.out.println("Sorted:");
    System.out.println(Arrays.toString(sorted.addSort(**HERE**)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM