如何在 Java 中不使用 nextLine() 的情况下在 next() 中包含空格
How to include white spaces in next() without using nextLine() in Java
我试图让用户输入一个字符串,它可以包含或不包含空格。因此,我正在使用 NextLine();
但是,我正在尝试使用该字符串搜索文本文件,因此我使用 next() 来存储扫描仪通过的每个字符串,我尝试使用 NextLine() 但它需要整行,我只需要逗号前的单词。
到目前为止,这是我的代码
System.out.print("Cool, now give me your Airport Name: ");
String AirportName = kb.nextLine();
AirportName = AirportName + ",";
while (myFile.hasNextLine()) {
ATA = myFile.next();
city = myFile.next();
country = myFile.next();
myFile.nextLine();
// System.out.println(city);
if (city.equalsIgnoreCase(AirportName)) {
result++;
System.out.println("The IATA code for "+AirportName.substring(0, AirportName.length()-1) + " is: " +ATA.substring(0, ATA.length()-1));
break;
}
}
当用户输入一个没有空格的单词时,该代码有效,但当他们输入两个单词时,不满足条件。
文本文件只包括一些机场、它们的 IATA、城市和国家。这是一个示例:
ADL, Adelaide, Australia
IXE, Mangalore, India
BOM, Mumbai, India
PPP, Proserpine Queensland, Australia
默认情况下,next() 搜索第一个空格作为分隔符。您可以像这样更改此行为:
Scanner s = new Scanner(input);
s.useDelimiter("\s*,\s*");
这样,s.next()
将匹配逗号作为您输入的分隔符(前面或后面有零个或多个空格)
查看 String#split 方法。
这是一个例子:
String test = "ADL, Adelaide, Australia\n"
+ "IXE, Mangalore, India\n"
+ "BOM, Mumbai, India\n"
+ "PPP, Proserpine Queensland, Australia\n";
Scanner scan = new Scanner(test);
String strings[] = null;
while(scan.hasNextLine()) {
// ",\s" matches one comma followed by one white space ", "
strings = scan.nextLine().split(",\s");
for(String tmp: strings) {
System.out.println(tmp);
}
}
输出:
ADL
Adelaide
Australia
IXE
Mangalore
India
BOM
Mumbai
India
PPP
Proserpine Queensland
Australia
我试图让用户输入一个字符串,它可以包含或不包含空格。因此,我正在使用 NextLine();
但是,我正在尝试使用该字符串搜索文本文件,因此我使用 next() 来存储扫描仪通过的每个字符串,我尝试使用 NextLine() 但它需要整行,我只需要逗号前的单词。 到目前为止,这是我的代码
System.out.print("Cool, now give me your Airport Name: ");
String AirportName = kb.nextLine();
AirportName = AirportName + ",";
while (myFile.hasNextLine()) {
ATA = myFile.next();
city = myFile.next();
country = myFile.next();
myFile.nextLine();
// System.out.println(city);
if (city.equalsIgnoreCase(AirportName)) {
result++;
System.out.println("The IATA code for "+AirportName.substring(0, AirportName.length()-1) + " is: " +ATA.substring(0, ATA.length()-1));
break;
}
}
当用户输入一个没有空格的单词时,该代码有效,但当他们输入两个单词时,不满足条件。
文本文件只包括一些机场、它们的 IATA、城市和国家。这是一个示例:
ADL, Adelaide, Australia
IXE, Mangalore, India
BOM, Mumbai, India
PPP, Proserpine Queensland, Australia
默认情况下,next() 搜索第一个空格作为分隔符。您可以像这样更改此行为:
Scanner s = new Scanner(input);
s.useDelimiter("\s*,\s*");
这样,s.next()
将匹配逗号作为您输入的分隔符(前面或后面有零个或多个空格)
查看 String#split 方法。
这是一个例子:
String test = "ADL, Adelaide, Australia\n"
+ "IXE, Mangalore, India\n"
+ "BOM, Mumbai, India\n"
+ "PPP, Proserpine Queensland, Australia\n";
Scanner scan = new Scanner(test);
String strings[] = null;
while(scan.hasNextLine()) {
// ",\s" matches one comma followed by one white space ", "
strings = scan.nextLine().split(",\s");
for(String tmp: strings) {
System.out.println(tmp);
}
}
输出:
ADL
Adelaide
Australia
IXE
Mangalore
India
BOM
Mumbai
India
PPP
Proserpine Queensland
Australia