In: Computer Science
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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; } } } } } }
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: