TheFoxterGirl
TheFoxterGirl

Reputation: 85

Compare two strings with numbers as words

I have been given numbers as words:

{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

Numbers are only up-to 10. And I my task is to compare given two input strings to each other.

It should basically work as you compare two numbers:

compare(1, 1) -> 0;
compare(1, 3) -> 1 < 3 as -1;
compare(5, 2) -> 5 > 2 as 1;

What would be the best suitable way to compare two strings like this?

Result would look something like this:

compare("one", "one") -> 0;
compare("one", "three") -> -1;
compare("five", "two") -> 1;
public int compare(String a, String b) {
    return 0;
}

Upvotes: 4

Views: 771

Answers (7)

Anis KCHAOU
Anis KCHAOU

Reputation: 1104

This method worked for me

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.List;

public class HelloWorld {
    static String[] wordsArray = {"one", "two", "three",
            "four", "five", "six", "seven", "eight",
            "nine", "ten"};

    public static void main(String[] args) {
        //test
        System.out.println(compare("one", "one"));
        System.out.println(compare("one", "three"));
        System.out.println(compare("five", "two"));
        System.out.println(compare("ten", "four"));
        System.out.println(compare("four", "ten"));
    }

    public static int compare(String a, String b) {
        // generate array of integer from 1 to 10
        int[] myIntArray = IntStream.range(1, wordsArray.length + 1)
                .toArray();

        //convert to string array
        String[] numbersArray = Arrays
                .toString(myIntArray)
                .split("[\\[\\]]")[1].split(", ");

        //concat word array and number array and convert them to list of string
        List<String> listWordsNumbers = Arrays.asList(
                Stream.concat(Stream.of(wordsArray),
                        Stream.of(numbersArray))
                        .toArray(String[]::new));

        //get index of each parameter in list and add 10 then compare indexes
        return Integer.compare(
                Integer.parseInt(
                        listWordsNumbers.get(listWordsNumbers.indexOf(a) + 10)),
                Integer.parseInt(
                        listWordsNumbers.get(listWordsNumbers.indexOf(b) + 10)));
    }
}

Output :

0
-1
1
1
-1

Upvotes: 1

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

Also, you can use switch:

public static void main(String[] args) throws Exception {
    System.out.println(compare("one", "one")); // 0
    System.out.println(compare("one", "three")); // -1
    System.out.println(compare("five", "two")); // 1
}

public static int compare(String first, String second) {
    return Integer.compare(toInt(first), toInt(second));
}

public static int toInt(String value) {
    return switch (value.toUpperCase()) {
        case "ONE" -> 1;
        case "TWO" -> 2;
        case "THREE" -> 3;
        case "FOUR" -> 4;
        case "FIVE" -> 5;
        case "SIX" -> 6;
        case "SEVEN" -> 7;
        case "EIGHT" -> 8;
        case "NINE" -> 9;
        case "TEN" -> 10;
        default -> 0;
    };
}

Upvotes: 1

user15495739
user15495739

Reputation:

You can use a single list of these numbers as words and compare the indices of its elements:

static List<String> list = Arrays.asList(
        "one", "two", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten");
static int compare(String a, String b) {
    return Integer.compare(list.indexOf(a), list.indexOf(b));
}
public static void main(String[] args) {
    System.out.println(compare("ten", "one")); // 1
    System.out.println(compare("one", "one")); // 0
    System.out.println(compare("one", "ten")); // -1

Upvotes: 1

WJS
WJS

Reputation: 40034

Here is yet another way.

String s = "onetwothreefourfivesixseveneightnineten";
int compare(String a, String b) {
    return Integer.compare(s.indexOf(a),s.indexOf(b));
}

Upvotes: 5

cMcNerlin
cMcNerlin

Reputation: 112

You can use a map and just pair the list of number-words with their integer value. Then, when you make comparisons, compare the integer values as you normally would rather than the actual words being entered.

If you were working with a large number of possibilities, this may be very tedious and impractical but for just 0-10 it's a reasonable solution.

Upvotes: 0

dreamcrash
dreamcrash

Reputation: 51433

You can use a map to code the Strings and their values. The benefit of this approach is that it has O(1) complexity as oppose to use of an array for instance.

Map<String, Integer> map = Map.of("one", 1, "two", 2, ...);

public int compare(String a, String b) {
      return Integer.compare(map.get(a),map.get(b));    
}

Full example:

public class Example {

    private final static Map<String, Integer> STRING_VALUE =
            Map.of("one", 1, "two", 2, "three", 3, "four", 4, "five", 5,
                    "six", 6, "seven", 7, "eight", 8, "nine", 9, "ten", 10);

    public static int compare(String a, String b) {
        return Integer.compare(STRING_VALUE.get(a),STRING_VALUE.get(b));
    }

   public static void main(String[] args) {
       System.out.println(compare("one", "one"));
       System.out.println(compare("one", "three"));
       System.out.println(compare("five", "two"));
    }
}

Output:

0
-1
1

Another solution is to use an ENUM:

Full Example:

public class Example {

    enum Values {
        ONE,
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN;
    }
    public static int compare(String a, String b) {
        Values vA = Values.valueOf(a.toUpperCase());
        Values vB = Values.valueOf(b.toUpperCase());
        return Integer.compare(vA.compareTo(vB), 0);
    }

   public static void main(String[] args) {
       System.out.println(compare("one", "one"));
       System.out.println(compare("one", "three"));
       System.out.println(compare("five", "two"));
    }
}

Output:

0
-1
1

Upvotes: 8

leech
leech

Reputation: 378

You can compare the position of the numbers you want to compare in the array. For example, if the inputs are "three" and "six", then "three" is in array[2] and "six" is in array[5]. 2 < 5, that means "three" < "six". Where array: String[] array = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

Upvotes: 1

Related Questions