[英]How to read input in java with slash using scanner?
I want to ask about how to get the value from user input input in java with scanner.我想问一下如何使用扫描仪从 java 中的用户输入中获取值。
eg the input is: 1000/2例如输入是:1000/2
i want to get like this a = 1000 and b = 2.我想得到这样的 a = 1000 和 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 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]);
You could use the splicing method.你可以使用拼接方法。 a = [YOUR VAR].substring([YOUR SPLICING POS])
a = [YOUR VAR].substring([YOUR SPLICING POS])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.