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

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 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 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 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())
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
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...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1)...
Hey! I'm having trouble answering this for my assignment. Thank you so much in advance. 1) Which type of vessels, arteries or veins, has more muscle fibers? What is the functional significance of this? 2a) In general, we have no conscious control over smooth muscle or cardiac muscle function, whereas we can consciously control to some extent all skeletal muscles. Can you consciously control your breathing? What does this tell you about the muscle type of the diaphragm? 2b) What...
Hi guys, I'm working on an assignment for my Java II class and the narrative for...
Hi guys, I'm working on an assignment for my Java II class and the narrative for this week's program has me a bit lost. I've put the narrative of the assignment in the quotation marks below. " Included with this assignment is an encrypted text file. A simple Caesar cipher was used to encrypt the data. You are to create a program which will look at the frequency analysis of the letters (upper and lowercase) to help “crack” the code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT