Question

In: Computer Science

I get an error in my code, here is the prompt and code along with the...

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:

  1. For each word in the input string, your program should search the dictionary string for the given word. If the word is not in the dictionary, you program should print the message "Unknown word found" to standard output.
  2. After traversing the entire input string, your program should print a message describing the total number of misspelled words in the string.
  3. A dictionary string may contain words in uppercase, lowercase, or a mixture of both. The test string may also combine upper and lower case letters. You program should recognize words regardless of case. So if "dog" is in the dictionary string, the word "dOG" should be recognized as a valid word. Similarly, if "CaT" is in the dictionary string, the word "cat" she be recognized as a valid word.
  4. Within a string, a word is a white-space delimited string of characters. So the last word in "Hello world!" is "world!".

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

Solutions

Expert Solution

/* 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!


Related Solutions

I can't get the number of days to print. Here is my code: public static void...
I can't get the number of days to print. Here is my code: public static void main(String[] args) { // Prompt the user to enter year Scanner scanner = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2016): "); int year = scanner.nextInt(); for(int i = 1; i <= 12; i++) printMonth(year, i); } /** Print the calendar for a month in a year */ static void printMonth(int year, int month) { // Print the...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint:...
I am doing a lab and I get this error message in my unittest: junit.framework.AssertionFailedError: Hint: matches() should have returned true when item matches the item passed into IdLookup constructor. Here is my code. What changes should be made? /** * This class is a lookup that matches items whose id matches exactly * a specified id, to be passed into the constructor when creating * the instance of the lookup. * * @author Franklin University * @version 2.0 */...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
I'm getting an error with my code on my EvenDemo class. I am supposed to have...
I'm getting an error with my code on my EvenDemo class. I am supposed to have two classes, Event and Event Demo. Below is my code.  What is a better way for me to write this? //******************************************************** // Event Class code //******************************************************** package java1; import java.util.Scanner; public class Event {    public final static double lowerPricePerGuest = 32.00;    public final static double higherPricePerGuest = 35.00;    public final static int cutOffValue = 50;    public boolean largeEvent;    private String...
I don't know why my java code is not running this error code pops up -->...
I don't know why my java code is not running this error code pops up --> Error: Could not find or load main class DieRoll Caused by: java.lang.ClassNotFoundException: DieRoll. Code below:    import java.util.Random;    import java.util.Scanner;    public class Assignment {    public static void combineStrings() {    String school = "Harvard";    String city = "Boston, MA";    int stringLen;       String upper, changeChar, combine;       stringLen = school.length();    System.out.println(school+" contains "+stringLen+" characters." );   ...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
I get an error 1452 saying it cannot add or update my foreign key. is there...
I get an error 1452 saying it cannot add or update my foreign key. is there a way to fix this
I can’t get my code to work and/or finish it. Please fix. Below are code instructions...
I can’t get my code to work and/or finish it. Please fix. Below are code instructions and then sample runs and lastly my code so far. //********************************************************************* Program You will write a program that uses a recursive function to determine whether a string is a character unit palindrome. Moreover, flags can be used to indicate whether to do case sensitive comparisons and whether to ignore spaces. For example "A nut for a jar of tuna" is a palindrome if spaces...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT