文件内容我想写一个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]);
}
}
}
}
我是初学者,所以请原谅我的错误。
从上面的代码输出 - 没有!没有错误,但没有输出!
我们不知道什么是错要么 - 也许你可以告诉我们发生了什么与你所期望的帮助。另外,如果这是作业,请标记为这样。 – 2012-02-08 20:49:29
首先是错误的是缩进。因为代码很难阅读。 – 2012-02-08 21:02:03