Read a word from a file

user9270836

If someone could help me figure out how to search if a word exists in a file, I would greatly appreciate it. I do know how to read an entire text file though.

And this is what I have so far:

public static void main(String[] args) throws IOException {
    File file = new File("words.txt");
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a word you would like to search for:");
    String word = sc.nextLine();

    List<String> words = new ArrayList<>();

    try {
        sc = new Scanner(file).useDelimiter( ",");

        while (sc.hasNext()) {
            final String wordFromFile = sc.nextLine();
            if (wordFromFile.contains(word)) {
                // a match!
                System.out.println("The entered word: " + word  + " exists in the dictionary");
                break;
            }

        }
    } catch (IOException e) {
        System.out.println(" cannot write to file " + file.toString());
    }
}

}

Ray

Just iterate through all the words in file an insert each into a HashSet from the file first. This is linear time O(n) to accomplish, no way around this as you got to read in the whole file.

Assuming one word from file it's like:

HashSet<String> set = new HashSet<>();
while (sc.hasNext()) {
    set.add(sc.nextLine();
}

If someone a sticker any they really want it read to a list type collection, you can generate a HashSet like this from the list:

 Set<String> set = new HashSet<>(wordList);

Note: This conversion operation is also O(n), so to read it into a list and convert you're O(2n), which is still O(n), but if this list is long far from optimal

For the lookup and/or insertion of the new word you check, can then do it in O(1) time.

if (set.contains(word)) {
   //...blah..blah...bla...
} else {
   set.add(word);
}

Hence the hash in the name HashSet.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

read word by word from file in C++

From Dev

Read File word by word

From Dev

Read a specific word from a text file and then save the next word

From Dev

Read word list from text file with control word length

From Dev

Read from a file search for a word and copy the entire line to another file

From Dev

How to read a file word by word

From Dev

How to read a single word (or line) from a text file Java?

From Dev

I need to make a unix script to read first word from a file

From Dev

C# Read every second word from txt file

From Dev

How to read int from file after specific word in Java

From Dev

How to read first word of last line from text file?

From Dev

Read specific Word(Line 2, Word 3) from a text file by batch script

From Dev

Read specific Word(Line 2, Word 3) from a text file by batch script

From Dev

How to find a word from a text file and then read the next word after it on C#

From Dev

Java 8 Streams: Read file word by word

From Dev

How to read a specified String from an input file, then read the next word after it

From Dev

Read Word file in JAVA/GWT

From Dev

Fstream doesnt save the last word in the file and it doesnt read from the file, too

From Dev

In python read from one specific word in a file to another specificword in a file multiple times

From Dev

How do I get my Hangman Java file to read a random word in from a .txt file?

From Dev

Split each line by a delim read from another file and store all split word compounds in the result file

From Dev

Read from text file and save word frequency to new text file printing each on a new line

From Dev

How to read word by word from an array of char?

From Dev

Word Count from a file

From Dev

How can I account for varying word lengths in a program that replaces words read from a text file?

From Dev

Read a word in a given position from every line in a text file using shell script

From Dev

how to read more than one word between double quotes from a file in C

From Dev

Read lines from a file and tokenize each line to extract specific word in shell script

From Dev

I can't compare correctly a word I read from a file with bufferedreader

Related Related

  1. 1

    read word by word from file in C++

  2. 2

    Read File word by word

  3. 3

    Read a specific word from a text file and then save the next word

  4. 4

    Read word list from text file with control word length

  5. 5

    Read from a file search for a word and copy the entire line to another file

  6. 6

    How to read a file word by word

  7. 7

    How to read a single word (or line) from a text file Java?

  8. 8

    I need to make a unix script to read first word from a file

  9. 9

    C# Read every second word from txt file

  10. 10

    How to read int from file after specific word in Java

  11. 11

    How to read first word of last line from text file?

  12. 12

    Read specific Word(Line 2, Word 3) from a text file by batch script

  13. 13

    Read specific Word(Line 2, Word 3) from a text file by batch script

  14. 14

    How to find a word from a text file and then read the next word after it on C#

  15. 15

    Java 8 Streams: Read file word by word

  16. 16

    How to read a specified String from an input file, then read the next word after it

  17. 17

    Read Word file in JAVA/GWT

  18. 18

    Fstream doesnt save the last word in the file and it doesnt read from the file, too

  19. 19

    In python read from one specific word in a file to another specificword in a file multiple times

  20. 20

    How do I get my Hangman Java file to read a random word in from a .txt file?

  21. 21

    Split each line by a delim read from another file and store all split word compounds in the result file

  22. 22

    Read from text file and save word frequency to new text file printing each on a new line

  23. 23

    How to read word by word from an array of char?

  24. 24

    Word Count from a file

  25. 25

    How can I account for varying word lengths in a program that replaces words read from a text file?

  26. 26

    Read a word in a given position from every line in a text file using shell script

  27. 27

    how to read more than one word between double quotes from a file in C

  28. 28

    Read lines from a file and tokenize each line to extract specific word in shell script

  29. 29

    I can't compare correctly a word I read from a file with bufferedreader

HotTag

Archive