Okay so i am trying to store all user input into a text file. Eg if a user enters a question i search the text file for that question. if the question is found the next line in the text file would be the answer to the question. So my output would be the found text + 1. For some reason my code just hangs when i am searching the file. For example, if a user enters "what is the name of the textbook?" the text file contains the question but I want to output "the textbook name is java" but the code hangs
好的,所以我试图将所有用户输入存储到文本文件中。例如,如果用户输入问题,我在该文本文件中搜索该问题。如果找到问题,文本文件中的下一行将是问题的答案。所以我的输出将是找到的文本+ 1.由于某种原因,我的代码只是在我搜索文件时挂起。例如,如果用户输入“教科书的名称是什么?”文本文件包含问题,但我想输出“教科书名称是java”,但代码挂起
Here is my code
这是我的代码
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();
}
and here is my text file
这是我的文本文件
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
4 个解决方案
#1
Can you once try this method with slight change?
你可以尝试这种方法稍作改动吗?
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.
}
}
}
Once it finds the question it only prints the next line.
一旦找到问题,它只打印下一行。
#2
lineFromFile
is declared as final String
and you are trying to add +1
to it, which you aren't allowed to do, especially because 1
is an int
not a string. This is why it is hanging.
lineFromFile被声明为final String,你试图向它添加+1,这是你不允许做的,特别是因为1是int而不是字符串。这就是它悬挂的原因。
Do this instead:
改为:
System.out.println(lineFromFile + " + 1" );
#3
The code will not hang on that line. try to change your code into this
代码不会挂在该行上。尝试将您的代码更改为此
input = scanner.next().toLowerCase();
Your first code is trying to retrieve the "next" line terminator. which would be the second enter if the user try to input a value.
您的第一个代码是尝试检索“下一个”行终止符。如果用户尝试输入值,这将是第二次输入。
#4
For what I know it should not hang... but let's go to the code first. lineFromFile + 1 has no special meaning and will not give you the next line. What you need is replace this line:
据我所知它不应该挂...但让我们先看看代码。 lineFromFile + 1没有特殊含义,也不会给你下一行。你需要的是替换这一行:
System.out.println(lineFromFile + 1);
by something like:
通过类似的东西:
if (scanner.hasNextLine()) {
final String response = scanner.nextLine();
System.out.println(response);
}
The logic is this: you already got to the question so get the next line (store it in response) and print it. Don't do that blindly as maybe you don't have a next line, check with scanner.hasNextLine() as you did in the while loop.
逻辑是这样的:你已经得到了问题,所以得到下一行(将其存储在响应中)并打印出来。不要盲目地这样做,因为你可能没有下一行,请像在while循环中一样检查scanner.hasNextLine()。
Update: on the hanging problem, it should not happen for what I know. Java has type conversions and concatenating a int literal to a String should result in a new String. String is immutable so the final modifier should not make the code hang. See https://docs.oracle.com/javase/tutorial/java/data/converting.html
更新:关于悬挂问题,它不应该发生在我所知道的事情上。 Java具有类型转换,并且将int文字连接到String应该产生新的String。字符串是不可变的,因此最终修饰符不应该使代码挂起。请参阅https://docs.oracle.com/javase/tutorial/java/data/converting.html
Anyway, the solution doesn't include that code so should not be a problem here.
无论如何,该解决方案不包括该代码,所以在这里不应该是一个问题。
#1
Can you once try this method with slight change?
你可以尝试这种方法稍作改动吗?
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.
}
}
}
Once it finds the question it only prints the next line.
一旦找到问题,它只打印下一行。
#2
lineFromFile
is declared as final String
and you are trying to add +1
to it, which you aren't allowed to do, especially because 1
is an int
not a string. This is why it is hanging.
lineFromFile被声明为final String,你试图向它添加+1,这是你不允许做的,特别是因为1是int而不是字符串。这就是它悬挂的原因。
Do this instead:
改为:
System.out.println(lineFromFile + " + 1" );
#3
The code will not hang on that line. try to change your code into this
代码不会挂在该行上。尝试将您的代码更改为此
input = scanner.next().toLowerCase();
Your first code is trying to retrieve the "next" line terminator. which would be the second enter if the user try to input a value.
您的第一个代码是尝试检索“下一个”行终止符。如果用户尝试输入值,这将是第二次输入。
#4
For what I know it should not hang... but let's go to the code first. lineFromFile + 1 has no special meaning and will not give you the next line. What you need is replace this line:
据我所知它不应该挂...但让我们先看看代码。 lineFromFile + 1没有特殊含义,也不会给你下一行。你需要的是替换这一行:
System.out.println(lineFromFile + 1);
by something like:
通过类似的东西:
if (scanner.hasNextLine()) {
final String response = scanner.nextLine();
System.out.println(response);
}
The logic is this: you already got to the question so get the next line (store it in response) and print it. Don't do that blindly as maybe you don't have a next line, check with scanner.hasNextLine() as you did in the while loop.
逻辑是这样的:你已经得到了问题,所以得到下一行(将其存储在响应中)并打印出来。不要盲目地这样做,因为你可能没有下一行,请像在while循环中一样检查scanner.hasNextLine()。
Update: on the hanging problem, it should not happen for what I know. Java has type conversions and concatenating a int literal to a String should result in a new String. String is immutable so the final modifier should not make the code hang. See https://docs.oracle.com/javase/tutorial/java/data/converting.html
更新:关于悬挂问题,它不应该发生在我所知道的事情上。 Java具有类型转换,并且将int文字连接到String应该产生新的String。字符串是不可变的,因此最终修饰符不应该使代码挂起。请参阅https://docs.oracle.com/javase/tutorial/java/data/converting.html
Anyway, the solution doesn't include that code so should not be a problem here.
无论如何,该解决方案不包括该代码,所以在这里不应该是一个问题。