Java 扫描仪 - 在扫描仪中使用条件来检查令牌类型和拆分字母数字
Java Scanner - Using conditions in scanner to check token type and splitting alphanumerals
我正在尝试将输入扫描器的两行字符串作为一个大字符串拆分为两个单独的字符串(如我下面的示例和预期输出所示)。
伪代码式代码
Scanner s = new Scanner("Fred: 18Bob D: 20").useDelimiter(":") //delimiter is probably pointless here
List<String> list = new ArrayList<>();
while (s.hasNext()) {
String str = "";
if (//check if next token is str) {
str = str + s.next();
}
if (//check if next token is :) {
//before the : will always be a name of arbitary token length (such as Fred,
//and Bob D), I also need to split "name: int" to "name : int" to achieve this
str = str + ": " + s.next();
}
if (//check if next token is alphanumeral) {
//split the alphanumeral then add the int to str then the character
str = str + s.next() + "\n" + s.next() //of course this won't work
//since s.next(will go onto the letter 'D')
}
else {
//more code if needed otherwise make the above if statement an else
}
list.add(str);
}
System.out.println(list);
预期输出
Fred: 18
Bob D: 20
我就是想不通如何才能做到这一点。如果可以给出实现此目标的任何指示,我将不胜感激。
另外,一个简短的问题。 \n
和 line.separator
有什么区别,我应该在什么时候使用它们?根据我在 class 代码中看到的简单示例,line.separator
已用于分隔 List<String>
中的项目,所以这是我唯一的经验。
您可以根据自己的目的尝试以下代码段:
List<String> list = new ArrayList<String>();
String str="";
while(s.hasNext()){
if(s.hasNextInt()){
str+=s.nextInt()+" ";
}
else {
String tmpData = s.next();
String pattern = ".*?(\d+).*";
if(tmpData.matches(pattern)){
String firstNumber = tmpData.replaceFirst(".*?(\d+).*", "");
str+=firstNumber;
list.add(str);
str="";
str+=tmpData.replace(firstNumber, "")+" ";
}else{
str+=tmpData;
}
}
}
list.add(str);
System.out.println(list);
我正在尝试将输入扫描器的两行字符串作为一个大字符串拆分为两个单独的字符串(如我下面的示例和预期输出所示)。
伪代码式代码
Scanner s = new Scanner("Fred: 18Bob D: 20").useDelimiter(":") //delimiter is probably pointless here
List<String> list = new ArrayList<>();
while (s.hasNext()) {
String str = "";
if (//check if next token is str) {
str = str + s.next();
}
if (//check if next token is :) {
//before the : will always be a name of arbitary token length (such as Fred,
//and Bob D), I also need to split "name: int" to "name : int" to achieve this
str = str + ": " + s.next();
}
if (//check if next token is alphanumeral) {
//split the alphanumeral then add the int to str then the character
str = str + s.next() + "\n" + s.next() //of course this won't work
//since s.next(will go onto the letter 'D')
}
else {
//more code if needed otherwise make the above if statement an else
}
list.add(str);
}
System.out.println(list);
预期输出
Fred: 18
Bob D: 20
我就是想不通如何才能做到这一点。如果可以给出实现此目标的任何指示,我将不胜感激。
另外,一个简短的问题。 \n
和 line.separator
有什么区别,我应该在什么时候使用它们?根据我在 class 代码中看到的简单示例,line.separator
已用于分隔 List<String>
中的项目,所以这是我唯一的经验。
您可以根据自己的目的尝试以下代码段:
List<String> list = new ArrayList<String>();
String str="";
while(s.hasNext()){
if(s.hasNextInt()){
str+=s.nextInt()+" ";
}
else {
String tmpData = s.next();
String pattern = ".*?(\d+).*";
if(tmpData.matches(pattern)){
String firstNumber = tmpData.replaceFirst(".*?(\d+).*", "");
str+=firstNumber;
list.add(str);
str="";
str+=tmpData.replace(firstNumber, "")+" ";
}else{
str+=tmpData;
}
}
}
list.add(str);
System.out.println(list);