-3

Have a very simple question here. Is there any way that I can convert below arguments to longs?

I want the absolute difference between the two different arguments but I end up with "java: integer number too large" error.

The problem, I guess, is that literals are always interpreted as integers. I thought recasting them as longs in the Diff() method would suffice?

Is there any way to concatenate/append the "L" (which converts integers to longs) to parameters?

I've been scratching my head for a while now... :/

public class Difference {
    public static void main(String[] args) {
        Diff(10,12);
        Diff(71293781758123,72784);
        Diff(1,12345677654321);
        Diff(1,2147483647);
    }

    public static Long Diff(long a, long b) {

        return Math.abs(a - b);

    }
}


Hambe
  • 1
  • 4
  • 5
    Just put an "L" at the end of the literals, e.g. 71293781758123L. – GriffeyDog Nov 16 '20 at 22:09
  • Always search Stack Overflow thoroughly before posting. – Basil Bourque Nov 16 '20 at 22:13
  • I'm looking for a way to do this where I don't pass an "L" at the end of the literals. The arguments have to look like they look. I can only manipulate the code in the Diff() method. – Hambe Nov 16 '20 at 22:25
  • **What** is the whole point of not passing the `L` suffix? – terrorrussia-keeps-killing Nov 16 '20 at 22:33
  • If by "The arguments have to look like they look. *I can only manipulate the code in the Diff() method*" you mean that you can't edit `Diff(71293781758123,72784);` then no, there is no way to make Java compiler see `71293781758123` as `long` without adding `l` or `L` suffix to ir. – Pshemo Nov 16 '20 at 22:34
  • Thank you! Then at least I know I'm not insane! – Hambe Nov 16 '20 at 22:37

1 Answers1

0

Instead of trying to append L, you can pass the parameters as String

public class Main {
    public static void main(String[] args) {
        System.out.println(diff("10", "12"));
        System.out.println(diff("71293781758123", "72784"));
        System.out.println(diff("1", "12345677654321"));
        System.out.println(diff("1", "2147483647"));
    }

    public static Long diff(String a, String b) {
        return Math.abs(Long.parseLong(a) - Long.parseLong(b));
    }
}

Output:

2
71293781685339
12345677654320
2147483646

Note: You should strive to follow the Java naming conventions e.g. the name of the method should be diff instead of Diff.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Why should the OP use strings "instead of trying to append L"? – terrorrussia-keeps-killing Nov 16 '20 at 22:13
  • @fluffy - The OP already knows that it can be solved by adding `L` at the end. His/her requirement is: `Is there any way to concatenate/append the "L" (which converts integers to longs) to parameters?` My answer is to suggest an alternative solution. – Arvind Kumar Avinash Nov 16 '20 at 22:17
  • So I forgot to say that the inputs HAVE to be numbers. That is I can't pass the parameters as Strings or add an L to the actual arguments. These arguments are passed on to my method by an external class that I have no control over (it's a web based test). Only a test question though so I'm not cheating or anything :) – Hambe Nov 16 '20 at 22:19
  • They were not clear on that: they mentioned _parameters_, not _arguments_. Additionally, the OP had numbers as integer tokens and none of them were suffixed with `L` therefore some of them could not fit the `int` range. – terrorrussia-keeps-killing Nov 16 '20 at 22:19
  • Are the parameters int's or long's? – NomadMaker Nov 16 '20 at 22:25
  • Well I've made them longs. The arguments are literals though so they are interpreted as integers. – Hambe Nov 16 '20 at 22:28
  • @Hambe - If the numbers are being passed to it from a web-based application, they may be from some textfield or some variables. In the first case (i.e. from some textfield), it will be easier for you to keep the parameters of type, `String`. In the second case, the calling method must already be using variables of type, `long` and therefore you need not worry about appending `L`. – Arvind Kumar Avinash Nov 16 '20 at 22:29
  • You were right about treating the input as text. I ended up using Scanner. Thanks for answering me! :) – Hambe Nov 18 '20 at 10:31