How to cast a String to several separate Java integers

0

I have one entry per console that has two separate integers but in a single line, that is, if the input is "2 4" I have to store in one variable the 2 and in another the 4. (These as int type ). For this I must use the BufferedReader library, I tried this but it does not compile me.

int n,m;
n = Integer.parseInt(sc.readLine());
m = Integer.parseInt(sc.readLine());

Thank you.

    
asked by ExpectoPatrom 05.11.2018 в 05:52
source

1 answer

1

Hello, you can use the split method which performs the division of a string using a parameter in this case would be the space. I'll leave you an example I hope you can adapt it to your program:

String entrada = "2 4";
String[] numeros = entrada.split(" ");
int numero1 = Integer.parseInt(numeros[0]);
int numero2 = Integer.parseInt(numeros[1]);

Greetings and good luck.

    
answered by 05.11.2018 в 09:29