0

我编写了以下代码,我的目标是在提示符处输入一个字符串并返回列表中包含该字符串的任何单词。我想我只是缺少一些小东西,因为当我运行程序时它会打印整个单词列表。任何帮助,将不胜感激。

package assignment1;

import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;

public class URLReader {
static List<String> words = new ArrayList<String>();

public static void main(String[] args) throws Exception {
    System.out.println("Please Input A String");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String letters = br.readLine();
    URL oracle = new URL("http://dl.dropbox.com/u/18678304/2011/BSc2/words.txt");
    BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        words.add(inputLine);
        in.close();

for(int i = 0; i<words.size(); i++)
{
    if(words.get(i).indexOf(letters) >= 0);
        System.out.println(words.get(i));
}

}
}
4

2 回答 2

2

;在 if 语句的末尾有一个:

if(words.get(i).indexOf(letters) >= 0);
    System.out.println(words.get(i));

它应该是:

if(words.get(i).indexOf(letters) >= 0)
    System.out.println(words.get(i));
于 2012-08-08T15:48:14.763 回答
2

一个语句不需要大括号 { },但始终使用它更安全:

if(words.get(i).indexOf(letters) >= 0){
System.out.println(words.get(i));
}
于 2012-08-08T15:56:46.430 回答