简体   繁体   中英

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:

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

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):

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);
          }
      }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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