-1

I have a spell check program that has these containing words:

Mary had a little lambb
Its fleece was white as ssnow
And everywhere that Mary wentt
The lamb was sure to ggo

The lambb, snnow wentt and ggo are purposely spelled like that.

I have written code that prints out the words that ARE found in the dictionary but cannot figure how to print out the words that AREN'T found.

Here is my code so far:

 package testDelimeter;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.Scanner;

public class test {
public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> dict = new ArrayList<String>();

    File  inFile = new File(
                     "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                     + "dict" + ".txt");
    Scanner in = new Scanner(inFile);

    File  text = new File(
                     "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                     + "text" + ".txt");
    Scanner s = new Scanner(text);

    while(in.hasNext()){
        dict.add(in.next());
    }

    while(s.hasNext()){
        String temp = s.next();

        for(int i = 0; i < dict.size(); i++){
            if(temp.equalsIgnoreCase(dict.get(i))){
                System.out.println(dict.get(i) + temp);
            }
        }
    }
}
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
ceri Westcott
  • 45
  • 2
  • 8
  • Just for information you can have a look at : https://code.google.com/p/google-api-spelling-java/ – Kartic Feb 21 '15 at 16:01

3 Answers3

1

Use a java.util.set for your dictionary. Check if an entry exists using the method .contains(..) of the java.util.collection interface implemented by List and Sets.

You shouldn't use a List as a dictionary because a dictionary has no order and can contain duplicate entries.

Flying Swissman
  • 818
  • 2
  • 14
  • 39
  • I think the primary problem that the OP is facing is how to print words not found. Your answer is asking him to use a different collection but does not address the actual question. The OP has already found a way to check if a word exists in the dictionary so your answer will only give him another way to do this. – Chetan Kinger Feb 21 '15 at 16:03
0

The best improvement you can make is changing you List for a Set. List is intended to be used when order is important and Set is intended to be used for unordered lists of unique elements. As you dictionary contains unique words, Set is better for your case.

Take a look at What is the difference between Set and List?

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

    Set<String> dict = new HashSet<String>();

    File inFile = new File(
            "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                    + "dict" + ".txt");
    Scanner in = new Scanner(inFile);

    File text = new File(
            "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                    + "text" + ".txt");
    Scanner s = new Scanner(text);

    while (in.hasNext()) {
        dict.add(in.next().toLowerCase());
    }

    while (s.hasNext()) {
        String temp = s.next().toLowerCase();

        if (dict.contains(temp)) {
            System.out.println("Found " + temp);
        } else {
            System.out.println("Not Found " + temp);
        }
    }
}

Edit: As OP wants to maintain lists, the code can be as follows:

public static void main(String[] args) throws FileNotFoundException {
    List<String> dict = new ArrayList<String>();

    File inFile = new File(
            "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                    + "dict" + ".txt");
    Scanner in = new Scanner(inFile);

    File text = new File(
            "C:\\Users\\Ceri\\workspace1\\testDelimeter\\src\\testDelimeter\\"
                    + "text" + ".txt");
    Scanner s = new Scanner(text);

    while (in.hasNext()) {
        // Notice toLowerCase()
        dict.add(in.next().toLowerCase());
    }

    while (s.hasNext()) {
        // Notice toLowerCase()
        String temp = s.next().toLowerCase();

        if (dict.contains(temp)) {
            System.out.println("Found " + temp);
        } else {
            System.out.println("Not Found " + temp);
        }
    }
}
Community
  • 1
  • 1
antonio
  • 18,044
  • 4
  • 45
  • 61
  • I know the difference but this is for a lab worksheet for University, we had to show that we understood how to use them regardless of how well they work. Thank you tough – ceri Westcott Feb 21 '15 at 16:07
  • @ceriWestcott See my answer. It does not change your data structure but only a small part of the logic. Isin't that what you want? If my answer solves your problem, don't forget to accept the answer and upvote. – Chetan Kinger Feb 21 '15 at 16:10
-1

A quick way to do this would be to introduce a boolean variable as shown below :

        boolean isWordFound;
        while (s.hasNext()) {
            String temp = s.next();
            isWordFound = false;
            for (int i = 0; i < dict.size(); i++) {
                if (temp.equalsIgnoreCase(dict.get(i))) {
                    isWordFound = true;
                    System.out.println("Found"+dict.get(i)+temp);
                    break;
                }
            }
            if (!isWordFound) {
                System.out.println("Could not find"+temp);
            }

        }

That being said, your program is a bit inefficient. Using a Map instead of list is the first change you can make.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82