user3235881
user3235881

Reputation: 497

Compare two long Strings

I want to compare two string who have those form

s1="1390785186301"
s2="1390785191301"

Shall I convert them to long and then compare with > or there are methods?

Upvotes: 0

Views: 3780

Answers (6)

Pshemo
Pshemo

Reputation: 124265

Depending on what you need you can check if both strings contains exactly same value with equals method (assuming no leading zeroes and that both numbers are represented using same notation, for instance "101" and "1.01E2" represent same value but strings are not equal).

But if you want to check which string contains bigger value String doesn't provide much help. It has its own compareTo method but it is using alphabetic order, which may fail if

  • strings are not same length: just like ab will be placed before b in dictionary, same rule applies to "12" "2" which means that 12 < 2 using this order
  • we are comparing negative values: example 12 < 13 in alphabetic order, but it also means -12 < -13.

It is much easier and safer to convert strings to proper numeric type like Long, BigInteger or even BigDecimal and use appropriate methods, like compareTo.

Demo

String s1 = "1390785186301";
String s2 = "1390785191301";

System.out.println(Long.compare(Long.parseLong(s1), Long.parseLong(s2))); // -1
System.out.println(new BigInteger(s1).compareTo(new BigInteger(s2)));     // -1

negative value for a.compareTo(b) indicates that a<b, 0 that a==b, positive that a>b

Demo 2:

BigDecimal bigDecimal = new BigDecimal("101");
BigDecimal bigDecimal2 = new BigDecimal("1.01E2");
System.out.println(bigDecimal.equals(bigDecimal2));    // true
System.out.println(bigDecimal.compareTo(bigDecimal2)); // 0

Upvotes: -1

Josh Engelsma
Josh Engelsma

Reputation: 2646

If you want to compare two strings, just use the compareTo method

if(s1.length() == s2.length()){    
    if(s1.compareTo(s2) > 0) {//then s1 is greater...}
}

Take a look at the javadoc

String#compareTo

Upvotes: 1

Tim B
Tim B

Reputation: 41208

The problem with using a string comparison is that in a string compare 12 comes before 9, not after it.

You will need to convert it to either a long (if it fits within the range of a 64 bit integer) or a BigInteger and then do the comparison using them.

For the long do:

 if (Long.parseLong(str1) > Long.parseLong(str2)) {
 }

or:

 int comparison = Long.compare(Long.parseLong(str1), Long.parseLong(str2));

The final option would be to do your own string comparator which scans from the start of the string comparing one digit at a time but if the strings are not equal length treats the shorter one as though it was left padded with 0.

Upvotes: 1

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

Here full ans

   public class Comparetion {
    public static void main(String args[]){
        String s1="1390785186301";
        String s2="1390785191301";


        if (s1.compareTo(s2) == 0) {
            System.out.println("s1 and S2 its same");
        }
        if (s1.compareTo(s2) > 0) {
            System.out.println("S1 is bigger then s2");
        }
        if (s1.compareTo(s2) < 0) {
            System.out.println("S2 is less then S1");
        }
    }

}

public class Comparetion {
    public static void main(String args[]){
        String s1="1390785186301";
        String s2="1390785191301";


        if (Long.parseLong(s1) < Long.parseLong(s2)) {
            System.out.println("s1 is less then s2");
        }
        else if(Long.parseLong(s1) > Long.parseLong(s2)){

            System.out.println("s1 is bigger then s2");
        }
        else{
            System.out.println("s1 and s2 are same.");

        }
    }

}

two possibilities. but in the case of Compare to will get exceptional cases.

Upvotes: 0

Alex K.
Alex K.

Reputation: 3304

Consider using this natural comparator, in case you are not sure, if there are digits only in your string.

Upvotes: 1

Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

if (new Long(s1) > new Long(s2))
    // do your thing

I see no reason for not creating longs for this.

Here's more info: http://docs.oracle.com/javase/6/docs/api/java/lang/Long.html#Long%28java.lang.String%29

Upvotes: -1

Related Questions