Java 中的分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5074730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delimiters in Java
提问by lqdc
I am trying to figure out how to use delimiters in Java. Basically, I am trying to parse input like this: "(x1, x2)" into double values. When I assign variables to the next double, the next integer gets counted as a double instead of the value between delimiters. How would I separate the values between delimiters and put them into variables?
我想弄清楚如何在 Java 中使用分隔符。基本上,我试图将这样的输入:“(x1, x2)”解析为双精度值。当我将变量分配给下一个双精度值时,下一个整数被计为双精度值,而不是分隔符之间的值。我如何将分隔符之间的值分开并将它们放入变量中?
System.out.print("Enter coordinates for two points as (x1, x2) (y1, y2): ");
Scanner input = new Scanner (System.in);
String wholeString = input.nextLine();
Scanner stringScanner = new Scanner (wholeString).useDelimiter("[,\s\(\)]*");
x1 = stringScanner.nextDouble();
x2 = stringScanner.nextDouble();
y1 = stringScanner.nextDouble();
y2 = stringScanner.nextDouble();
slope = (y2 - y1) / (x2 - x1);
回答by Sergey Vedernikov
try to use this delimiter: [,\\s\\(\\)]+
尝试使用此分隔符: [,\\s\\(\\)]+
*
means 0or more
*
意味着0或更多