比较用户输入与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.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]);
}
}
}
}
我是初学者,所以请原谅我的错误。
从上面的代码输出 - 没有!没有错误,但没有输出!
我无法理解你正在尝试在上面的代码做(也许是我的脑子是不是由于缺乏睡眠的作用),但你可以做到以下几点:
- 获取用户输入并将其存储在
String input
中。 - 像上面那样逐行读取文件,但使用
StringTokenizer
并标记每行。 - 只需将第一个标记与输入进行比较,看看它们是否匹配。
- 如果是,打印行;退出,否则转到下一行。
你似乎已经得到了你的输入,使用你的扫描仪类你所需要做的就是添加一个HashMap。
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html
东西线沿线的,
更新
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!";
}
}
}
希望这有助于
人,你没有使用泛型?此外,这不会编译,因为如果响应为空,则不会返回任何内容。哦,一个HashMap有一个.containsKey(key)方法,所以你不需要对它进行空检查。 – 2012-02-08 21:10:40
@Zenzen:虽然我明显同意泛型,但做一个空检查比使用containsKey更有效,因为它只需要一次地图查找而不是两次。 – 2012-02-08 21:22:35
是的,但出于某种奇怪的原因,在一个键下可能有一个空值,并且一个简单的get将不会区分它;) – 2012-02-08 21:55:50
我们不知道什么是错要么 - 也许你可以告诉我们发生了什么与你所期望的帮助。另外,如果这是作业,请标记为这样。 – 2012-02-08 20:49:29
首先是错误的是缩进。因为代码很难阅读。 – 2012-02-08 21:02:03