I want to ask about how to get the value from user input input in java with scanner.
eg the input is : 1000/2
i want to get like this a = 1000 and b = 2.
thank you in advance.
I want to ask about how to get the value from user input input in java with scanner.
eg the input is : 1000/2
i want to get like this a = 1000 and b = 2.
thank you in advance.
Try this.
Reader reader = new StringReader("1000/2");
try (Scanner in = new Scanner(reader).useDelimiter("/")) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println("a = " + a + ", b = " + b);
}
output
a = 1000, b = 2
Try This.
String s[] = scannerObject.next().split("/"); // split the input at once
int a = Integer.parseInt(s[0]); // convert string to integer
int b = Integer.parseInt(s[1]);