简体   繁体   English

用Java编写程序以从用户读取多个字符串并与文本文件进行比较

[英]Writing a program in Java to read in multiple strings from user and compare to text file

I am attempting to write a program that will take user input ( a long message of characters), store the message and search a text file to see if those words occur in the text file. 我正在尝试编写一个程序,该程序将接受用户输入(长字符消息),存储消息并搜索文本文件以查看这些单词是否出现在文本文件中。 The problem I am having is that I am only ever able to read in the first string of the message and compare it to the text file. 我遇到的问题是我只能读取消息的第一个字符串并将其与文本文件进行比较。 For instance if I type in "learning"; 例如,如果我输入“学习”; a word in the text file, I will get a result showing that is is found in the file. 在文本文件中的一个单词,我将得到一个结果显示在文件中找到。 However if I type "learning is" It will still only return learning as a word found in the file even though "is" is also a word in the text file. 但是,如果我输入“学习是”它仍然只会将学习作为文件中的单词返回,即使“是”也是文本文件中的单词。 My program seems to not be able to read past the blank space. 我的程序似乎无法读取空白区域。 So I suppose my questions is, how do I augment my program to do this and read every word in the file? 所以我想我的问题是,如何扩充我的程序来执行此操作并阅读文件中的每个单词? Would it also be possible for my program to read every word, with or without spaces, in the original message taken from the user, and compare that to the text file? 我的程序是否也可以在用户的​​原始消息中读取包含或不包含空格的每个单词,并将其与文本文件进行比较?

Thank you 谢谢

import java.io.*;
import java.util.Scanner;

public class Affine_English2

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

        String message = "";
        String name = "";

        Scanner input = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        System.out.println("Please enter in a message: ");
        message = scan.next();

        Scanner file = new Scanner(new File("example.txt"));


          while(file.hasNextLine())
           {
              String line = file.nextLine();

              for(int i = 0; i < message.length(); i++)
              {
                  if(line.indexOf(message) != -1)
                  {
                      System.out.println(message + " is an English word ");
                      break;
                  }
              }

            }

    }

}

You may try with this. 你可以试试这个。 With this solution you can find each word entered by the user in your example.txt file: 使用此解决方案,您可以在example.txt文件中找到用户输入的每个单词:

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

    String message = "";
    String name = "";

    Scanner input = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter in a message: ");
    message = scan.nextLine();

    Scanner file = new Scanner(new File("example.txt"));

    while (file.hasNextLine())
    {
      String line = file.nextLine();

      for (String word : message.split(" "))
      {
        if (line.contains(word))
        {
          System.out.println(word + " is an English word ");
        }
      }
    }
  }

As Mark pointed out in the comment, change 正如马克在评论中指出的那样,改变

scan.next();

To: 至:

scan.nextLine();

should work, i tried and works for me. 应该工作,我试着为我工作。

If you can use Java 8 and Streams API 如果可以使用Java 8和Streams API

public static void main(String[] args) throws Exception{ // You need to handle this exception

    String message = "";
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter in a message: ");
    message = input.nextLine();

    List<String> messageParts = Arrays.stream(message.split(" ")).collect(Collectors.toList());

    BufferedReader reader = new BufferedReader(new FileReader("example.txt"));

    reader.lines()
            .filter( line -> !messageParts.contains(line))
            .forEach(System.out::println);

}

I recommend you first process the file and build a set of legal English words: 我建议您先处理文件并构建一组合法的英文单词:

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

  Set<String> legalEnglishWords = new HashSet<String>();
  Scanner file = new Scanner(new File("example.txt"));

  while (file.hasNextLine()) {
    String line = file.nextLine();

    for (String word : line.split(" ")) {
            legalEnglishWords.add(word);
        }
    }

    file.close();

Next, get input from the user: 接下来,从用​​户那里获得输入:

    Scanner input = new Scanner(System.in);
    System.out.println("Please enter in a message: ");
    String message = input.nextLine();
    input.close();

Finally, split the user's input to tokens and check each one if it is a legal word: 最后,将用户的输入拆分为令牌,并检查每个令牌是否为合法词:

    for (String userToken : message.split(" ")) {
        if (legalEnglishWords.contains(userToken)) {
            System.out.println(userToken + " is an English word ");
        }
    }
  }
}

You have many solution, but when it comes to find matches I suggest you to take a look to the Pattern and Matcher and use Regular Expression I haven't fully understood your question, but you could do add something like this (I did not tested the code but the idea should work fine): 你有很多解决方案,但是当找到匹配时,我建议你看一下Pattern和Matcher并使用正则表达式我还没有完全理解你的问题,但你可以添加这样的东西(我没有测试过)代码,但想法应该工作正常):

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

    String message = "";
    String name = "";

    Scanner input = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter in a message: ");
    message = scan.next();

    Scanner file = new Scanner(new File("example.txt"));

    String pattern = "";

    for(String word : input.split(" ")){
        pattern += "(\\b" + word + "\\b)";
    }

    Pattern r = Pattern.compile(pattern);

      while(file.hasNextLine())
       {
          String line = file.nextLine();
          Matcher m = r.matcher(line);

          if(m.matches()) {
               System.out.println("Word found in: " + line);
          }
      }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM