使用Java将用户输入与文件内容进行比较
我想写一个java代码,它可以读取一个包含许多句子的文件-
Hey how you doing?
Hi I am fine.
Hello World, Good Morning!
要求用户输入句子的第一个单词作为输入,输出应该是句子的其余部分。例如,如果输入字是“Hi”,那么输出应该是“我很好”下面是我的代码,我不知道怎么了!真的需要你们的帮助!谢谢
import java.io.*;
import java.util.*;
import java.io.FileReader;
import java.io.BufferedReader;
public class FileContents {
public static void main(String args[]) {
BufferedReader br=new BufferedReader(new FileReader("myfile.txt"));
Vector lineArray=new Vector();
String lineContents=null;
int counter=0,i;
try {
while ((lineContents=br.readLine())!=null) {
lineArray.add(lineContents);
counter++;
}
} catch (FileNotFoundException fne) {
fne.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
Scanner input = new Scanner(System.in);
int no=3;
String[] textData=new String[no];
for (i=0;i<no;i++) {
textData[i]=br.readLine();
}
br.close();
System.out.println("These are the file contents : ");
for (i=0;i<lineArray.size();i++) {
System.out.println(lineArray.get(i));
}
System.out.println("\n Enter first word of sentence : ");
String st = input.nextLine();
String[] word= st.split(" ");
System.out.println("\n Rest of the sentence is : ");
for (i=0;i<lineArray.size();i++) {
if (word[i].equals(textData[i])) {
while (word[i]!='\n')
System.out.println(word[i]);
}
}
}
}
我是初学者,请原谅我的错误
以上代码的输出-无!!没有错误,但也没有输出
# 1 楼答案
我无法理解您在上述代码中试图做什么(可能是我的大脑因睡眠不足而无法正常工作),但您可以执行以下操作:
String input
中李>StringTokenizer
并标记每一行李># 2 楼答案
您似乎已经下载了输入,使用scanner类只需添加一个HashMap即可
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html
类似于
更新
你需要用你的回答填充你的回答图
更新2
这就是我将如何重新组织你们的课堂,我省略了代码,因为这是你们的家庭作业
希望这有帮助