如何在文件中搜索句子+1?
How to search for a sentence +1 in a file?
好的,所以我正在尝试将所有用户输入存储到一个文本文件中。例如,如果用户输入一个问题,我会在文本文件中搜索该问题。如果找到问题,文本文件中的下一行将是问题的答案。所以我的输出将是找到的文本 + 1。由于某种原因,我的代码在搜索文件时挂起。例如,如果用户输入 "what is the name of the textbook?" 文本文件包含问题但我想输出 "the textbook name is java" 但代码挂起
这是我的代码
public static void parseFile(String s) throws IOException {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
// a match!
System.out.println(lineFromFile + 1); // code hangs right here.
}
}
}
public static void getinput() throws IOException {
Scanner scanner = new Scanner(System.in);
String input = null;
/* End Initialization */
System.out.println("Welcome ");
System.out.println("What would you like to know?");
System.out.print("> ");
input = scanner.nextLine().toLowerCase();
parseFile(input);
}
public static void main(String args[]) throws ParseException, IOException {
/* Initialization */
getinput();
}
这是我的文本文件
the current assignment is 4.5
the class is near
the lecture is at four
what is the textbook name?
the textbook name is Java
the major is difficult
the number of test are six
lineFromFile
被声明为 final String
并且您正试图向其中添加 +1
,这是不允许的,尤其是因为 1
是一个int
不是字符串。这就是它挂起的原因。
改为这样做:
System.out.println(lineFromFile + " + 1" );
据我所知,它不应该挂起...但是让我们先看代码。 lineFromFile + 1没有特殊意义,不会给你下一行。你需要的是替换这一行:
System.out.println(lineFromFile + 1);
通过类似的方式:
if (scanner.hasNextLine()) {
final String response = scanner.nextLine();
System.out.println(response);
}
逻辑是这样的:您已经回答了问题,所以获取下一行(将其存储在 response 中)并打印出来。不要盲目地这样做,因为您可能没有下一行,请像在 while 循环中那样检查 scanner.hasNextLine()。
更新:关于挂起的问题,据我所知应该不会发生。 Java 具有类型转换,将 int 文字连接到 String 应该会产生一个新的 String。 String 是不可变的,所以 final 修饰符不应该让代码挂起。参见 https://docs.oracle.com/javase/tutorial/java/data/converting.html
无论如何,解决方案不包含该代码,因此这里应该不是问题。
代码不会挂在那行。试着把你的代码改成这个
input = scanner.next().toLowerCase();
您的第一个代码试图检索 "next" 行终止符。如果用户尝试输入值,这将是第二次输入。
你能稍微改变一下这个方法吗?
public static void parseFile(String s) throws IOException {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
System.out.println(scanner.nextLine()); // code hangs right here.
}
}
}
一旦找到问题,它只会打印下一行。
好的,所以我正在尝试将所有用户输入存储到一个文本文件中。例如,如果用户输入一个问题,我会在文本文件中搜索该问题。如果找到问题,文本文件中的下一行将是问题的答案。所以我的输出将是找到的文本 + 1。由于某种原因,我的代码在搜索文件时挂起。例如,如果用户输入 "what is the name of the textbook?" 文本文件包含问题但我想输出 "the textbook name is java" 但代码挂起
这是我的代码
public static void parseFile(String s) throws IOException {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
// a match!
System.out.println(lineFromFile + 1); // code hangs right here.
}
}
}
public static void getinput() throws IOException {
Scanner scanner = new Scanner(System.in);
String input = null;
/* End Initialization */
System.out.println("Welcome ");
System.out.println("What would you like to know?");
System.out.print("> ");
input = scanner.nextLine().toLowerCase();
parseFile(input);
}
public static void main(String args[]) throws ParseException, IOException {
/* Initialization */
getinput();
}
这是我的文本文件
the current assignment is 4.5
the class is near
the lecture is at four
what is the textbook name?
the textbook name is Java
the major is difficult
the number of test are six
lineFromFile
被声明为 final String
并且您正试图向其中添加 +1
,这是不允许的,尤其是因为 1
是一个int
不是字符串。这就是它挂起的原因。
改为这样做:
System.out.println(lineFromFile + " + 1" );
据我所知,它不应该挂起...但是让我们先看代码。 lineFromFile + 1没有特殊意义,不会给你下一行。你需要的是替换这一行:
System.out.println(lineFromFile + 1);
通过类似的方式:
if (scanner.hasNextLine()) {
final String response = scanner.nextLine();
System.out.println(response);
}
逻辑是这样的:您已经回答了问题,所以获取下一行(将其存储在 response 中)并打印出来。不要盲目地这样做,因为您可能没有下一行,请像在 while 循环中那样检查 scanner.hasNextLine()。
更新:关于挂起的问题,据我所知应该不会发生。 Java 具有类型转换,将 int 文字连接到 String 应该会产生一个新的 String。 String 是不可变的,所以 final 修饰符不应该让代码挂起。参见 https://docs.oracle.com/javase/tutorial/java/data/converting.html
无论如何,解决方案不包含该代码,因此这里应该不是问题。
代码不会挂在那行。试着把你的代码改成这个
input = scanner.next().toLowerCase();
您的第一个代码试图检索 "next" 行终止符。如果用户尝试输入值,这将是第二次输入。
你能稍微改变一下这个方法吗?
public static void parseFile(String s) throws IOException {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
System.out.println(scanner.nextLine()); // code hangs right here.
}
}
}
一旦找到问题,它只会打印下一行。