Question

In: Computer Science

I'm having problems with my java code not correctly spellchecking certain words. 1) Determine if a...

I'm having problems with my java code not correctly spellchecking certain words.

1) Determine if a word entered by the user is spelled correctly. A word is considered correct if it's found in dictionary.txt (see required output to get file).

these are the input and output that are suppose to come out i already have the dictionary.txt i just don't know hoe to set it up someone please help me

Standard Input                 Files in the same directory
glimmer
hello
world
test
tommorrow
tomorrow
recluse
habittat
exit
  • dictionary.txt
Output
Enter word to spellcheck (or exit to end)\n
glimmer is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
hello is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
world is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
test is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
tommorrow is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
tomorrow is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
recluse is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
habittat is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
Ending program...\n

Test Case 2

Standard Input                 Files in the same directory
fries
abc
apple
zzzzzzzzz
a
exit
  • dictionary.txt
Output
Enter word to spellcheck (or exit to end)\n
fries is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
abc is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
apple is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
zzzzzzzzz is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
a is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
Ending program...\n

Test Case 3

Standard Input                 Files in the same directory
rocking
chair
rocking chair
hieroglyphics
lie
detector
lie detector
exit
  • dictionary.txt
Output
Enter word to spellcheck (or exit to end)\n
rocking is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
chair is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
rocking chair is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
hieroglyphics is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
lie is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
detector is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
lie detector is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
Ending program...\n

Test Case 4

Standard Input                 Files in the same directory
mailbox
mailing
miling list
mailan
mail order
maim
exit
  • dictionary.txt
Output
Enter word to spellcheck (or exit to end)\n
mailbox is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
mailing is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
miling list is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
mailan is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
mail order is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
maim is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
Ending program...\n

Test Case 5

Standard Input                 Files in the same directory
NE
neard
nearby
nearly
nearsghted
neat
exit
  • dictionary.txt
Output
Enter word to spellcheck (or exit to end)\n
NE is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
neard is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
nearby is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
nearly is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
nearsghted is not spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
neat is spelled correctly.\n
Enter word to spellcheck (or exit to end)\n
Ending program...\n

Here's the current java code that I am using (in IntelliJ):

import java.lang.*;
import java.util.*;
import java.io.*;

public class PS6LoopsPart2Q5p2 {

    public static void main(String[] args) throws Exception
    {
        RandomAccessFile br=new RandomAccessFile("dictionary.txt","r");
        Scanner input = new Scanner(System.in);
        String s;


        while(true)
        {

            System.out.println("Enter word to spellcheck (or exit to end)");

            String key=input.nextLine();
            if (key.equalsIgnoreCase("exit"))
            {
                System.out.println("Ending program...");
                System.exit(0);
            }

            else
            {
                while ((s=br.readLine())!=null){
                    if(!s.contains(key))
                    {
                        System.out.println(key+" is spelled correctly.");
                        break;
                    }
                    else
                    {
                        System.out.println(key+" is not spelled correctly.");
                        break;
                    }


                }
            }
        }
    }
}

Solutions

Expert Solution

code:

import java.util.*; //to use Scanner class
import java.io.*; //to use file operations
import java.lang.*;


public class PS6LoopsPart2Q5p2{
public static void main(String[] args) throws Exception{
  
RandomAccessFile br=new RandomAccessFile("C:\\Users\\Rupa\\Desktop\\dictionary.txt","r");
Scanner input = new Scanner(System.in);
String s;
boolean x;
int found;


while(true)
{

System.out.println("Enter word to spellcheck (or exit to end)");

String key=input.nextLine();
if (key.equalsIgnoreCase("exit"))
{
System.out.println("Ending program...");
System.exit(0);
}

else
{
found = 0; // for every time we initially set found as 0
br.seek(0); // Again moving the cursor to initial position
while((s=br.readLine())!=null){
if(s.equals(key)) // equals method is used to comapre two strings
{
found = 1;
System.out.println(key+" is spelled correctly.");
break;
}
}
if(found==0)
{
System.out.println(key+" is not spelled correctly.");
}
}
}

}   
}

OUTPUT:


Related Solutions

Thanks in advance. In Java. I'm having an issue with one of my methods. Ex: The...
Thanks in advance. In Java. I'm having an issue with one of my methods. Ex: The array being pass in is a character array with element: w, ' ', K, Q, k, q, ' ', -, ' ', 0, ' ', 1 public class Find { public void enPassant(char[] array) {   for(int i = 0; i < array.length; ++i) { if(array[i] == 'e') { count = i; } else { count += 0; } }    if(count > 0) {...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
I'm having trouble with validating this html code. Whenever I used my validator, it says I...
I'm having trouble with validating this html code. Whenever I used my validator, it says I have a stray end tag: (tbody) from line 122 to 123. It's the last few lines. Thanks and Ill thumbs up whoever can help solve my problem. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <title>L7 Temperatures Fields</title> <!--    Name:    BlackBoard Username:    Filename: (items left blank for bb reasons    Class Section: (blank)    Purpose: Making a table to demonstrate my...
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when...
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when ever another function is called from the class. Can someone proof read this for me and let me know what'd up? Header.h file #include <iostream> using namespace std; class report {    private:        int* num_stu;         int xx;        int* id;        double* gpa;    public:                report(int x) {             num_stu = &x;             xx = x;            ...
I'm having trouble with my do while loop. I'm trying to get it where if the...
I'm having trouble with my do while loop. I'm trying to get it where if the user enter's 3 after whatever amount of caffeinated beverages they've entered before then the loop will close and the rest of my code would proceed to execute and calculate the average price of all the caffeinated beverages entered. Can someone please help me with this? Here's my Code: import java.util.Scanner; public class Main { public static void main(String[] args) { CaffeinatedBeverage[] inventory = new...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
I'm having trouble getting a certain output with this code #include <stdio.h> //function prototypes void initializeArray(int...
I'm having trouble getting a certain output with this code #include <stdio.h> //function prototypes void initializeArray(int size, int ids[]); void printArray(int size, int * idPointer); int main(void) { // 1. declare an array of 5 integers called ids int ids[5]; // 2. declare an integer pointer called arrayPointer and // initialize it to point to the array called ids int *arrayPointer = ids; // 3. call initializeArray() function sending to it // 5 for the size and the array called...
I'm writing a code for the Secant Method for root finding, but my code makes an...
I'm writing a code for the Secant Method for root finding, but my code makes an infinite code of the correct answer. Below is my code, how do I fix it? def f(x): return (x**6)+7*(x**5)-15*(x**4)-70*(x**3)+75*(x**2)+175*x-125 def secant(): p0=float(input('Enter an initial guess for the root ')) p1=float(input('Enter another guess ')) TOL=float(input('Enter a tolerance in decimal form ')) n=15 i=1 while i<=n: p=p1-f(p1)*(p1-p0)/(f(p1)-f(p0)) if abs(p-p1)<TOL: print(p) else: p0=p1 p1=p i=i+1 else: print(p) return p    print(secant())
I'm having a bit of an issue with one of my C++ labs. It's not for...
I'm having a bit of an issue with one of my C++ labs. It's not for score but I need to understand it so I can move on. A restaurant servers burgers for $8 and salads for $7. When the customer selects a burger they must choose if they would like cheese. If they do, cheddar costs an additional 25 cents while pepper jack is 50 cents. When the customer selects a salad they must choose if they would like...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT