0

My homework for a Java is making a GUI calculator in Java. I am in the planning of the program process and wondering how can you add a number next to another number and make it a single integer. I know there is probably a name for this but i cant remember it or i just don't know it.

if x = 1, and y = 2, then z should equal 12

How do i do this?

Thanks in advance!

javasux
  • 9
  • 1

2 Answers2

0

int z = Integer.parseInt(x+""+y) is the correct approach. If we need to take a negative number in the calculator application, the -ve sign will be before the first digit entered, which works well here. Also in Calculator only one digit will be added at one time, so this approach works for negative numbers as well.

  • Isn't that a repetition of my answer? – Andronicus Mar 11 '19 at 16:25
  • @Andronicus It includes more explanation (which is a very good ting). On the other hand is partially wrong about the negative sign (unary minus): it will work well if `x` is negative, but not if `y` is. – Ole V.V. Mar 12 '19 at 05:45
  • Yes it will fail if y is -ve but in the calculator when entering one number it will be a unary +ve or -ve number where only x can be -ve not any other digit. – Neelam Agarwal Mar 12 '19 at 17:22
0

I believe this is what you're looking for.

    int x = 1;
    int y = 2;
    String answer = x + "" + y;
    int z = Integer.parseInt(answer);

    System.out.println(z); //Should print 12
Adan Vivero
  • 422
  • 12
  • 36