使用 Java 在文本文件中搜索字符串
Searching for a string in a text file using Java
我试图让用户输入机场名称,程序将从文本文件中搜索以获取匹配的代码,现在我只需要它来读取该行。我在这里调查了许多类似的问题,但对我没有任何帮助。程序 return else 结果而不是 found 结果。
到目前为止,这是我的代码
public static void main (String[] args) throws IOException
{
File file = new File("codes01.dat");
Scanner myFile = new Scanner(file);
Scanner kb = new Scanner(System.in);
String line;
System.out.println("Hey man, this is totally leigt, just enter an Airport code and I'll hook you up.");
System.out.print("First, y'all need to give me the file name: ");
String fileName = kb.nextLine();
if (fileName.equalsIgnoreCase("codes01"))
{
System.out.print("Cool, now give me your Airport Name: ");
String AirportName = kb.nextLine();
while (myFile.hasNextLine())
{
line = myFile.nextLine();
String name = myFile.next();
System.out.println(line);
if(name.equalsIgnoreCase(AirportName))
{
System.out.println("IT'S WORKING, I DON'T KNOW HOW BUT IT IS "+line);
break;
}
else
{
System.out.println("Sorry, dude, ain't no airport in my VERY limited list with that name");
break;
}
}
}
The program return the else result rather than the found result.
那是因为您在测试文件中的第一行后就跳出了循环。
仔细查看您的代码...在上下文中。
if(name.equalsIgnoreCase(AirportName)) {
System.out.println("It is working");
break;
} else {
System.out.println("Sorry");
break; // What????
}
为什么要在 if-else 块中使用 break 语句?尝试摆脱 break 语句,然后执行您的代码。
if(name.equalsIgnoreCase(AirportName))
{
System.out.println("The name of Airport matches");
}
else
{
System.out.println("Sorry No Match Found");
}
仔细看这两行。那里有问题。假装自己是 Scanner
class.
在脑海中逐步执行代码
line = myFile.nextLine();
String name = myFile.next();
我试图让用户输入机场名称,程序将从文本文件中搜索以获取匹配的代码,现在我只需要它来读取该行。我在这里调查了许多类似的问题,但对我没有任何帮助。程序 return else 结果而不是 found 结果。
到目前为止,这是我的代码
public static void main (String[] args) throws IOException
{
File file = new File("codes01.dat");
Scanner myFile = new Scanner(file);
Scanner kb = new Scanner(System.in);
String line;
System.out.println("Hey man, this is totally leigt, just enter an Airport code and I'll hook you up.");
System.out.print("First, y'all need to give me the file name: ");
String fileName = kb.nextLine();
if (fileName.equalsIgnoreCase("codes01"))
{
System.out.print("Cool, now give me your Airport Name: ");
String AirportName = kb.nextLine();
while (myFile.hasNextLine())
{
line = myFile.nextLine();
String name = myFile.next();
System.out.println(line);
if(name.equalsIgnoreCase(AirportName))
{
System.out.println("IT'S WORKING, I DON'T KNOW HOW BUT IT IS "+line);
break;
}
else
{
System.out.println("Sorry, dude, ain't no airport in my VERY limited list with that name");
break;
}
}
}
The program return the else result rather than the found result.
那是因为您在测试文件中的第一行后就跳出了循环。
仔细查看您的代码...在上下文中。
if(name.equalsIgnoreCase(AirportName)) {
System.out.println("It is working");
break;
} else {
System.out.println("Sorry");
break; // What????
}
为什么要在 if-else 块中使用 break 语句?尝试摆脱 break 语句,然后执行您的代码。
if(name.equalsIgnoreCase(AirportName))
{
System.out.println("The name of Airport matches");
}
else
{
System.out.println("Sorry No Match Found");
}
仔细看这两行。那里有问题。假装自己是 Scanner
class.
line = myFile.nextLine();
String name = myFile.next();