[英]Scanning 2 Different Data Types Java
I have a data file that is a list of names followed by "*****" and then continues with integers. 我有一个数据文件,该文件是名称列表,后跟“ *****”,然后以整数继续。 How do I scan the names and then break with the asterisks, followed by scanning the integers?
如何扫描名称,然后打上星号,然后扫描整数?
This question might help : Splitting up data file in Java Scanner 该问题可能会有所帮助: 在Java扫描仪中拆分数据文件
Use the Scanner.useDelimiter() method, put "*****" as the delimiter, like this for example : 使用Scanner.useDelimiter()方法,将“ *****”作为分隔符,例如:
sc.useDelimiter("*****");
OR 要么
Alternative : 替代方案:
Below code should work for you 下面的代码应该为您工作
Scanner scanner = new Scanner(<INPUT_STR>).useDelimiter("[*****]");
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
// For Integer
} else {
// For String
}
}
Although this seems a tedious thing, I think this would solve the issue without worrying if the split returns anything, and the out of bounds. 尽管这看起来很乏味,但我认为这可以解决问题,而不必担心拆分是否返回任何内容以及超出范围。
final String x = "abc****12354";
final Pattern p = Pattern.compile("[A-Z]*[a-z]*\\*{4}");
final Matcher m = p.matcher(x);
while (m.find()) {
System.out.println(m.group());
}
final Pattern p1 = Pattern.compile("\\*{4}[0-9]*");
final Matcher m1 = p1.matcher(x);
while (m1.find()) {
System.out.println(m1.group());
}
The first pattern match minus the last 4 stars (can be substring-ed out) and the second pattern match minus the leading 4 stars (also can be removed) would give the request fields. 第一个模式匹配项减去最后4个星号(可以进行子字符串删除),第二个模式匹配项减去前4个星号(也可以删除)将提供请求字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.