检查扫描仪输入是否等于转换为arraylist的列表

时间:2014-10-09 07:48:11

标签: java list

我有一个包含大量字符串的文件。我已将该文件中的任何内容转换为列表。现在我想创建一个扫描用户输入的扫描程序,并返回true或false,不管该扫描程序输入是否包含列表(文件)中的内容。

我脑子里有逻辑,但我不知道语法。

我走到这一步。我需要帮助。提前致谢。

public static void main (String [] args) throws Exception {

    Scanner scan = new Scanner(new File("/Users/Greg/workspace/erlang/fred.txt"));
    ArrayList<String> list = new ArrayList<String>();
    while (scan.hasNext()){
        list.add(scan.next());
        Scanner scan2 = new Scanner (System.in);

        System.out.println("Enter");

        String input = scan2.nextLine();

       // if input equals list / file - > sysout true . else > false

}

在stackoverflow上的神奇人物的帮助下,我完成了我的任务。完成的代码就是这样,它运作得很好:¨

public static void main(String[] args) throws Exception {
    Scanner scan = new Scanner(new File("/Users/Greg/workspace/erlang/fred.txt"));
    ArrayList<String> list = new ArrayList<String>();
    while (scan.hasNext()) {
        list.add(scan.next());

    }
    do {
    String input = null;

    Scanner scan2 = new Scanner(System.in);

    input = scan2.nextLine();
    if (list.contains(input)) {
        System.out.println("Yes, List/File contains: " + input);
    }
    else {
        System.out.println("No, List/File does not contain: " + input);
    }
    } while (true);

}

{

}

{

}

2 个答案:

答案 0 :(得分:0)

我创建了一个BufferedReader,它从System.in读取输入,然后检查列表是否包含已读入的行,并相应地写出来。

public static void main (String [] args) throws Exception {    
    Scanner scan = new Scanner(new File("/Users/Greg/workspace/erlang/fred.txt"));
    ArrayList<String> list = new ArrayList<String>();
    while (scan.hasNext()){
        list.add(scan.next());

    BufferedReader bufferedReader = null;
    String line = null;
    try
    {
        bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        do
        {
            line = bufferedReader.readLine();
            System.out.println("" + list.contains(line));
        }
        while(!"exit".equals(line));
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(bufferedReader != null) try { bufferedReader.close(); } catch(IOException e) {}
    }
}

答案 1 :(得分:0)

public static void main (String [] args) throws Exception {

Scanner scan = new Scanner(new File("/Users/Greg/workspace/erlang/fred.txt"));
ArrayList<String> list = new ArrayList<String>();
while (scan.hasNext()){
    list.add(scan.next());
 }

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


try {
        System.out.println("EnterValue : ");
        String s = br.readLine();
        while (!s.equals("quit")) {
            System.out.println("EnterValue : ");
            boolean flag = false;
            for (String value : list) {
                if (value.equals(s)) {
                    flag = true;
                    break;
                }
                if (flag) {
                    System.out.println("true");
                } else {
                    System.out.println("false");
                }

            }
            s = br.readLine();
        }
    } catch (IOException e) {
    }