Add number at the end of another number without adding - JAVA

0

I must do a method that allows you to add a number to the end of another number without adding it.

For example, I have 15253 and 99, I must add that 99 at the end of 15253, without adding it, that is, it should be 1525399.

    
asked by Lucas Bukato 24.05.2018 в 20:24
source

2 answers

2

You can do it that easily.

String valorStr = "";
int valor = 15253; 
int resultado = 99;

valorStr = valor + "" + resultado;  // "1525399"

valor = Integer.parseInt(valorStr); // 1525399  
    
answered by 24.05.2018 в 20:40
2

You can concatenate both values if they are of the String type, therefore you must first convert them to this type.

int valor = 15253; 
int resultado = 99;
String cadenaResultante = String.valueOf(15253) +   String.valueOf(99);

here you would get a string that contains "1525399", now if you want to convert this value to numeric use the method Integer.parseInt () , like this:

int numeroFinal = Integer.parseInt(cadenaResultante); 

to get a whole value 1525399

If you want a method with the above, it would be done this way:

private int concatenarNumeros(int valor, int resultado){
   String cadenaResultante = String.valueOf(valor) +   String.valueOf(resultado);
   return  Integer.parseInt(cadenaResultante); 
}

you can call the method this way:

int valor = concatenarNumeros(15253, 99);

Obviously the String value to be converted to integer must not exceed the values: 2147483647 or -2147483647

    
answered by 24.05.2018 в 20:45