In: Computer Science
I get an error in my code, here is the prompt and code along with the error.
Write a spell checking program (java) which uses a dictionary of words (input by the user as a string) to find misspelled words in a second string, the test string. Your program should prompt the user for the input string and the dictionary string. A valid dictionary string contains an alphabetized list of words.
Functional requirements:
CODE:
dictionary.java
import java.io.*;
import java.util.*;
public class dictionary
{
public static void main(String args[]) throws Exception
{
String dString;
String uString;
Scanner input=new Scanner(System.in);
System.out.println("Enter distionary string :");
dString = input.nextLine();
System.out.println("Enter input string :");
uString = input.nextLine();
String[] dict = dString.split(" ");//split dictionay
string and save to array fo string
boolean found = false;
//ieterate over dictionary string array
for (String a : dict)
{
//compare and print message
accordingly
if((uString.toLowerCase().compareTo(a.toLowerCase())) == 0)
{
System.out.println("word found!");
found =
true;
break;
}
}
if(found == false)
{
System.out.println("Unknown word
found!");
}
}
}
ERROR:
Main.java:4: error: class dictionary is public, should be
declared in a file named dictionary.java
public class dictionary
^
1 error
compiler exit status 1
/* First save the class as dictionary.java
* The program prompts user to enter the dictionary string and then
prompt input string to search in the previous dictionary
* string. If the input string is found then print the input string
is found otherwise print unknown word found.
* */
//dictionary.java
import java.util.Scanner;
public class dictionary
{
public static void main(String args[]) throws
Exception
{
String dString;
String uString;
Scanner input=new
Scanner(System.in);
System.out.println("Enter
distionary string :");
dString = input.nextLine();
System.out.println("Enter input
string :");
uString = input.nextLine();
String[] dict = dString.split("
");//split dictionay string and save to array fo string
boolean found = false;
//ieterate over dictionary string
array
for (String a : dict)
{
//compare and
print message accordingly
if((uString.toLowerCase().compareTo(a.toLowerCase())) == 0)
{
System.out.println("word found!");
found = true;
break;
}
}
if(found == false)
{
System.out.println("Unknown word found!");
}
}
}
------------------------------------------------------------------------------------------------------------------
Sample Output:
Enter distionary string :
watch seeing
Enter input string :
watch
word found!