0

I'm trying to write a Comparator for sorting a string list based on the integers it has. ex. H3232GHSD3 and H56RFRSFR4, the first string has integers 32323, while the second string has integers 564, therefore the second string is less than the first.

Heres my Code

import java.util.*;

// Sorts strings based on integers it contains 
class IntComparator implements Comparator<String>{

    @Override
    public int compare(String s1, String s2) {
        // Strip the non integers from strings
        s1 = s1.replaceAll("[^\\d.]","");
        s1 = s1.replaceAll("[^\\d.]","");
        // change string to integers
        int l1 = Integer.parseInt(s1);
        int l2 = Integer.parseInt(s2);

        if(l1 > l2){
            return 1;
        }
        else if(l1 < l2){
            return -1;
        }
        return 0;
    }
}
public class sample {
    public static void main(String[] args) {

        List<String> RandomString = new ArrayList<String>();

        RandomString.add("HA4ZNV0WE1");
        RandomString.add("A3XHN20WE1");
        RandomString.add("D4VH3V0WE1");

        Collections.sort(RandomString, new IntComparator());

        for(String R : RandomString){
            System.out.println(R);
        }

    }

}

and this is the error I get

Exception in thread "main" java.lang.NumberFormatException: For input string: "HA4ZNV0WE1"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at IntComparator.compare(sample.java:13)
    at IntComparator.compare(sample.java:1)
    at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.TimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at java.util.Collections.sort(Unknown Source)
    at sample.main(sample.java:36)

Thanks,

Dreadlock
  • 35
  • 1
  • 5

4 Answers4

3

You have a typo in your code. Change -

// Strip the non integers from strings
s1 = s1.replaceAll("[^\\d.]","");
s1 = s1.replaceAll("[^\\d.]","");

to this -

// Strip the non integers from strings
s1 = s1.replaceAll("[^\\d.]","");
s2 = s2.replaceAll("[^\\d.]",""); // In your code, you've written s1 here too.

I am assuming you've copied and pasted your first line, and forgot to change the variable name. That's why sometimes it is called an anti-pattern.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
2

Notice your code

// Strip the non integers from strings
s1 = s1.replaceAll("[^\\d.]","");
s1 = s1.replaceAll("[^\\d.]","");

You should make the second line into

s2 = s2.replaceAll("[^\\d.]","");

In the code you posted, you're just getting rid of the non-digits in s1 twice, and never making s2 point to a string with only digits.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
0

try [^\p{L}] instead of you regex

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

try

        s1 = s1.replaceAll("\\D", "");
        s2 = s2.replaceAll("\\D", "");
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275