3

Two strings representing two numbers have been provided as input. The numbers in the string can be so large that they may not be represented by the Java data type int. The objective is to compare the two numbers and output that number as a string

for example we have to compare:

"874986754789289867753896798679854698798789857387687546456"

and

"98347598375689758967756458678976893478967586857687569874"

which both are out of range of long and int data types in JAVA and after comparing we have to output that number as a string

Akshay Sharma
  • 75
  • 1
  • 1
  • 9

6 Answers6

9

you could start by first looking at each string's length. if one of them is longer and you know they are both unsigned values, the longer string has the bigger number. if they both have the same length, you start comparing the strings char by char starting from left to right. when you found your first bigger digit you conclude that number is bigger.

gpu3d
  • 1,164
  • 1
  • 10
  • 21
2

Whether you're trying to compare or subtract isn't clear, but Java's BigInteger class has both operations. This is probably what you need:

BigInteger a = new BigInteger("874986754789289867753896798679854698798789857387687546456");
BigInteger b = new BigInteger("98347598375689758967756458678976893478967586857687569874");

System.out.println( (a.compareTo(b) > 0 ? "a" : "b") + " is larger.");

If you need to subtract the numbers I'd definitely use BigInteger, but if all you need to do is compare them you could write your own method. One of your input strings is longer than the other, so barring leading zeroes that would tell you right away which is larger. If the strings are the same length you could compare them character by character in a loop to see which is larger. The String charAt and toCharArray methods give you two different ways to implement this approach.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
0

If it is school assignment in which you can't use built-in solutions but you need to write your own method to compare such strings then you need to

  1. check sign of numbers (if one is negative and one positive then answer is obvious)
  2. remove leading zeroes - 0000123 represents 123
  3. after removing leading zeroes compare length of numbers (longer one among positive numbers is larger)
  4. if both numbers have same length iterate over them from left to right until you will find two different digits (if you will not find any numbers are equal).

Members of String class you may need

  • toCharArray()
  • charAt(int index)
  • length

In case you can use built-in solutions you can use these strings to create BigInteger instances and use its compareTo method which will tell if one number is larger (positive result) equal to (0) or smaller (negative result) than other.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

In order to use numbers that big, they must be declared as double. You can use parsers to transform Strings into numbers.

public static void main(String [] args) {
    String numberOne = "874986754789289867753896798679854698798789857387687546456";
    double parsedNumberOne = Double.parseDouble(numberOne);

    String numberTwo = "98347598375689758967756458678976893478967586857687569874";
    double parsedNumberTwo = Double.parseDouble(numberTwo);

    double result = compareMethod (numberOne, numberTwo);
    System.out.println("Result = " + result);
}

I declared the numbers explicitly and not by reading user input, but you can modify that easily to be inline with your code. In the compareMethod, you can compare those number anyway you like.

Regards

0

Just comparing two strings using compareTo() is more than enough. The comparison will be done by converting each character into its ASCII value.

String number1 = "874986754789289867753896798679854698798789857387687546457";
String number2 = "874986754789289867753896798679854698798789857387687546456";

int result = number1.compareTo(number2);
if (result == 0) {
    System.out.println("Both are equal");
} else if (result > 0) {
    System.out.println("Number1 is greater");
} else {
    System.out.println("Number2 is greater");
}
Loganathan Mohanraj
  • 1,736
  • 1
  • 13
  • 22
-1

You need to compare characters (converted to numbers) one by one (or group by group) from right to left. Use methods like charAt(int) or substring(int, int) respectively.

WrongASP
  • 257
  • 1
  • 7
  • 1
    Actualy, one can compare from left to right. If the two numbers are the same length then the first larger digit determines which number is larger. (And if they aren't the same length, and neither has leading zeros, then the longer number is the larger.) – Hot Licks Aug 23 '14 at 13:48
  • Use BigDecimal or BigInteger .. don't re-implement the wheel (with a possible mistake) – ErstwhileIII Aug 23 '14 at 13:59
  • @HotLicks You're right. I haven't actually thought of that. It is much more efficient. – WrongASP Aug 23 '14 at 14:37