使用Java将用户输入与文件内容进行比较
我想写一个java代码,它可以读取一个包含许多句子的文件-使用Java将用户输入与文件内容进行比较,java,Java,我想写一个java代码,它可以读取一个包含许多句子的文件- Hey how you doing? Hi I am fine. Hello World, Good Morning! 要求用户输入句子的第一个单词作为输入,输出应该是句子的其余部分。例如,如果输入单词是“Hi”,那么输出应该是“我很好”。这是我下面的代码,我不知道出了什么问题!真的需要你们的帮助!谢谢 import java.io.*; import java.util.*; import java.io.FileReade
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]);
}
}
}
}
import java.io.*;
导入java.util.*;
导入java.io.FileReader;
导入java.io.BufferedReader;
公共类文件内容{
公共静态void main(字符串args[]){
BufferedReader br=新的BufferedReader(新文件读取器(“myfile.txt”);
向量线性阵列=新向量();
字符串lineContents=null;
int计数器=0,i;
试试{
而((lineContents=br.readLine())!=null){
lineArray.add(lineContents);
计数器++;
}
}catch(FileNotFoundException fne){
fne.printStackTrace();
}捕获(io异常){
io.printStackTrace();
}
扫描仪输入=新扫描仪(System.in);
int no=3;
字符串[]文本数据=新字符串[否];
对于(i=0;i我无法理解您在上述代码中尝试执行的操作(可能是由于睡眠不足,我的大脑无法正常工作),但您可以执行以下操作:
获取用户输入并将其存储在字符串输入中
像上面那样逐行读取文件,但使用StringTokenizer
并标记每一行
只需将第一个令牌与输入进行比较,看看它们是否匹配
如果是,打印行;退出,否则转到下一行
您似乎已经完成了输入,使用scanner类只需添加一个HashMap即可
类似于
更新
HashMap responseMap = new HashMap();
public string genResponse(String word) {
String response = (String) responseMap.get(word);
if (response != null){
return response;
} else {
return "Word unrecognized!"
}
}
您需要用您的响应填充您的响应图
更新2
这就是我将如何重新组织你们的课堂,我省略了代码,因为这是你们的家庭作业
public class FileContents {
public HashMap responseMap = new HashMap();
public Vector lineArray = new Vector();
public static void main(String args[]){
//Call function to populate the map
populateResponseMap();
//Read the lines
readLines();
}
public void populateResponseMap(){
//This is where you would add responses, something like
responseMap.put("Hi", "I am fine");
}
public void readLines() {
//This is where you would read the data from the file using you scanner class
//With each line you would call this function
genResponse(//first word in line );
}
public String genResponse(String word) {
String response = (String) responseMap.get(word);
if (response != null) {
return response;
} else {
return "Word unrecognized!";
}
}
}
希望这有帮助我们也不知道哪里出了问题——也许你可以告诉我们发生了什么,而不是你期望的。另外,如果这是作业,请这样标记。第一个错误是缩进。因为代码很难阅读。各位,你不使用泛型吗?这也不会编译,因为你没有返回任何代码如果响应为null.Oh并且HashMap有一个.containsKey(键)的话方法,所以您不需要对其进行空值检查。@Zenzen:虽然我显然同意泛型,但执行空值检查比执行containsKey更有效,因为它只需要一次映射查找,而不是两次。是的,但出于某种奇怪的原因,键下可能有空值,而简单的get无法区分这一点;)@garyamoris-我是一个初学者,抱歉,我不知道在我的代码中添加这些行。如果你发布完整的代码并添加行,那就太好了。再次感谢!我真的不应该只发布一个答案,但是我会添加一个更新,说明你应该如何构造这些行。记住使用函数,它们会让你的代码如此更具可读性。