I have started making a calculator. There are buttons and a screen where the numbers should how up. Except I dont know how to program the larger numbers so when someone types in a '5' then a '2' then a '7' the number becomes 527 instead of a 5 then a 2 then a 7.

How would I make the number just add on after the previous one?

Thanks for the help.

hai friend
i think this may help you
store the result of the first operation in a variable and add the coming numbers to that variable
this may solve your problem

If you have a variable with a number '5' and someone enters a '2', you can do <varname>=<varname>*10+input, which will store 52.

Try storing your variable as a String.

So that when user enters 5 your variable = "5". Then after user enters 2 you do

var = var + input;

which will concatenate 5 and 2 to make "52".

Then before you do your computation, you need to cast this String into an int

int i = Integer.parseInt(variable);

Then you can perform math operations on int i.

Simple string concatenation is enough. Store numbers as string. and use + operator to concatenate.

If you are using numeric data types for storing variables then multiply it with 10 and add new input.

Storing it in a String would not be a good idea because the OP's making a calculator, so he will probably have to do various operations that can only be performed on numerical datatypes (e.g. addition, subtraction). If you directly put it in an int, there's no need for casting back and forth.

Edited 6 Years Ago by coil: n/a

This article has been dead for over six months. Start a new discussion instead.