2

EDIT: The comparator doesn't seem to be doing anything. I get no errors but the (already alphabetically sorted) array isn't changing. I should expect to see SAD and PAD beside each other but I am not.

I have an array of strings and I want to sort them based on their numerical value derived from how to type them out on a dial pad. That is, "SAD" = 723, "PAD" = 723, "SAP" = 727. I have an alphabetically ordered dictionary and I want to rearrange it into my order in order to do a binary search on the array. For things like SAD and PAD being the same value, they can be then sorted alphabetically. I want to try to use Arrays.sort(dictionary, myComparator) to sort it.

First, would this be the best way to go about getting what I want? How else might I get this?

Second, how do I make my own comparator? Note that I am just in a first course in programming so we aren't even making classes yet, it's all just a bunch of stuff crammed into a main class. And I generally don't know about classes at all. So if I would have to go and make my own class to make this work, then I will probably have to code a quicksort myself instead. I haven't covered linked lists either so my data structure is a bit clumsy. Here is what I have so far and I have no idea what I am doing:

Stupid clumsey way to get the dictionary into memory, important line is near bottom:

public static String[][] dictToMem()throws FileNotFoundException{
    File myFile = new File("src/words.txt");
    Scanner input = new Scanner(myFile);
    String temp;
    String[] tempDict = new String[7];
    Arrays.fill(tempDict, "");

    while (input.hasNext()){
        temp = input.next();
        if (temp.length() < 8 && temp.indexOf('\'') + temp.indexOf('(') +   temp.indexOf('-') == -3){
            for (int i = 1; i <= 7; i++){
                if (temp.length() == i){
                    tempDict[i-1] += " " + temp;
                }
            }
        }
    }
    for (int i = 0; i<tempDict.length; i++){
        tempDict[i] = tempDict[i].trim();
    }

    String[][] dictionary = new String[7][];
    for (int i = 0; i<tempDict.length; i++){
        dictionary[i] = tempDict[i].split(" ");
    }
    for (int i = 0; i<tempDict.length; i++){
        //  ***This is the line doesn't work, compiler error***
        //  dictionary[i] = Arrays.sort(dictionary[i], String.DialPadNumCompare);
        //  Should be this:
        Arrays.sort(dictionary[i], DialPadNumCompare);
    }
    return dictionary;
}

And the comparator isn't showing an error:

public static Comparator<String> DialPadNumCompare = new Comparator<String>(){
    @Override
    public int compare(String a, String b){
        if (stringToInt(a) == stringToInt(b)){
            return a.compareTo(b);
        }
        return stringToInt(b) - stringToInt(a);
    }
};

String to int is my string to dial pad number converting method

5
  • 1
    Well, What compiler error? Commented Nov 25, 2012 at 5:35
  • cannot find symbol symbol: variable DialPadNumCompare location: class String Commented Nov 25, 2012 at 5:43
  • Try to adhere to java naming conventions of variable names start with lower case Commented Nov 25, 2012 at 5:57
  • @ Bohemian Where did I not do that? Is "DialPadNumCompare" a variable? Commented Nov 25, 2012 at 6:01
  • The sort with comparator is not doing anything. The program runs and goes through the sort but the dictionary looks the same before and after the sort, where I should be seeing BAN and CAN together. And why did I get downvoted? Commented Nov 25, 2012 at 6:03

2 Answers 2

3

Your comparator is not correct. Change it to:

public static Comparator<String> dialPadNumCompare = new Comparator<String>(){
    @Override
    public int compare(String a, String b){
        int inta = stringToInt(a);
        int intb = stringToInt(b);
        if (inta == intb)
            return a.compareTo(b);
        return inta - intb;
    }
};

And call this comparator using this code:

Arrays.sort(dictionary, dialPadNumCompare);

Here is Live Demo

Sign up to request clarification or add additional context in comments.

Comments

0

For your immediate error, change the line:

Arrays.sort(dictionary[i], String.DialPadNumCompare);

To:

Arrays.sort(dictionary[i], DialPadNumCompare);

The way you have it now, you are trying to statically reference a non-existent member of the String class. Instead you should be trying to reference the static reference DialPadNumCompare that you created in your own class.

2 Comments

Now I'm getting a problem that probably doesn't have to do with the comparator. Doing the change you suggest gives this: incompatible types required: String[] found: void. Also, the condition on that for loop should be i<dictionary[i].length, not tempDict.length
The Arrays class does a sort-in-place, and doesn't have a return type. Change your line dictionary[i] = Arrays.sort(dictionary[i], DialPadNumCompare); to Arrays.sort(dictionary[i], DialPadNumCompare);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.