Question

I want to compare 2 strings with eachother with the compareTo() function.

example:

int result = "650".compareTo("651");
    if(result < 0){
        System.out.println("smaller");
    } else if(result > 0){
        System.out.println("bigger");
    } else {
        System.out.println("equals");
    }
    System.out.println(result);

this will output smaller which is correct.

example 2:

int result = "650".compareTo("1000");
    if(result < 0){
        System.out.println("smaller");
    } else if(result > 0){
        System.out.println("bigger");
    } else {
        System.out.println("equals");
    }
    System.out.println(result);

This will return as output bigger. Which is kinda strange as 650 as number is smaller than 1000.

How's that and how can i change it? (yes the numbers need to be in text format).

I want to do this:

int result = "650/65".compareTo("1050/50");
    if(result < 0){
        System.out.println("smaller");
    } else if(result > 0){
        System.out.println("bigger");
    } else {
        System.out.println("equals");
    }

This returns that "650/65" is bigger than "1050/50" yet in fact it is smaller.

EDIT

I've worked out the cases and this is how it is now:

String maat = "650/65";
    int subMaatB = 0;
    int subMaatA = 0;

    if(maat.contains("/")){
        try{
            subMaatA = Integer.parseInt(maat.substring(0, maat.lastIndexOf("/")));
            subMaatB = Integer.parseInt(maat.substring(maat.lastIndexOf("/")+1));
        } catch (NumberFormatException e){

        }
    }

    boolean resultA = subMaatA >= 440;
    boolean resultB = subMaatB >= 65;
    boolean resultC = subMaatA <= 1050;
    boolean resultD = subMaatB <= 50;
    if(resultA && resultB && resultC && resultD){
        System.out.println("BIGGER");
    } else {
        System.out.println("Smaller");
    }

So the case is, Maat has to be within a range of 440/65 and 1050/50. Maat should be between the range i've mentioned before.

Était-ce utile?

La solution

"3".compareTo("2") is as good as "three".compareTo("two")

Meaning they are both string comparisons.

String.compareTo()

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

this.charAt(k)-anotherString.charAt(k) If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:

You must convert them to integers first if you want to compare them as integers.

Autres conseils

You are comparing Strings in these examples not integer numbers. The result will be alphabetic comparsion in this case. And '6' is > '1' no matter what the subsequent characters are.

String comparison compares the Strings lexicographically, so "6..." is bigger than "1...". To compare numeric values you should convert your Strings to a numeric type

You need to convert your strings to numbers via (say) Integer.parseInt(String) prior to comparison.

At the moment you're comparing the actual string contents. That doesn't care whether you've written 1000. It won't interpret it as a number.

Try this

int inputOne = Integer.parseInt("650");
int inputTwo = Integer.parseInt("1000");
int result = inputOne - inputTwo;
    if(result < 0){
        System.out.println("smaller");
    } else if(result > 0){
        System.out.println("bigger");
    } else {
        System.out.println("equals");
    }
    System.out.println(result);

You can not compare two digits as String, with the compareTo() Method.

compareTo()

The compareTo() Method uses a value that is based on unicode. That means if you use the compareTo() Method it beasically sort it in a alphabetical order. Furthermore the compareTo() Method is mostly only used for values, with an implementet Comperable interface.

If you want to compare two strings, you have to use

string.equals(comparestring);

For your example you have to convert the string into a Integer. You do that like so:

Integer.parseInt(string);

If you want it back to a String because you want to put it in a String LinkedList or just as an output, use:

string.toString();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top