Question: Im getting stuck on this final line of code: STEP#6 USING AN ENHANCED FOR LOOP ASSIGN EVERY WORD FROM YOUR jWords LIST INTO A VAR
Im getting stuck on this final line of code:
STEP#6 USING AN ENHANCED FOR LOOP ASSIGN EVERY WORD FROM YOUR jWords LIST INTO A VAR NAMED jWord FOR (.......) { print jWord and a " " ( NOT a NEWLINE YET) // ASSUME JWORD IS: "sopt" jCanon = toCanonical( jWord ); // CANONICAL WILL BE: "opst" CALL THE BINARY SEARCH METHOD IN THE COLLECTIONS LIBRARY FEED THE BINARYSEARCH TWO VALUES: YOUR dCanons list AND YOUR jCanon word index = Collections.binarySearch( dCanons, jCanon ); IF THE INDEX COMES BACK NEGATIVE IT MEANS NO MATCH FOUND. THERE ARE NO DICT WORDS THAT ARE EQUIVALENT TO THIS JUMBLED WORD. YOU CAN PRINT A NEWLINE. ELSE LETS ASSUME bsearch returns you the number 19 LOOK IN THE LISTS ABOVE YOU CAN SEE THAT BSEARCH BROUGHT YOU AN INDEX THAT IS IN THE MIDDLE OF A CHUNK OF HITS YOU MUST WRITE A LITTLE LOOP OR TWO THAT DOES THE FOLLOWING WALK BACKWARD TILL YOU FIND THE VERY TOPMOST dCanon word that equals jCanon "opst" THEN START LOOPING DOWNWARD PRINTING EACH DICT WORD AT SAME INDEX IN THE OTHER LIST WHEN YOU REACH A jCanons WORD THAT IS NO LONGER equals TO YOUR jCanon word PRINT A NEWLINE. (YOU ARE DONE WITH THI JUMBLED WORD GO BACK UP AN DO THe NEXT ONE) } FINAL OUTPUT IS atc cat tac atr art rat tar gdo dog god grof frog sopt opts post pots spot stop tops sygols
My Code:
import java.util.*;
import java.io.*;
public class Project4
{
public static void main(String[] args) throws Exception
{
if (args.length < 1)
{
System.out.println(" usage: C:\\> java Project4 dictionary.txt jumbles.txt ");
System.exit(0);
}
BufferedReader dfile = new BufferedReader( new FileReader(args[0]) );
ArrayList
pairs.add("");
while ( dfile.ready() )
{
String line = dfile.readLine();
pairs.add(line);
}
dfile.close();
Collections.sort(pairs);
ArrayList
for (String line: pairs)
{
String [] pair = line.split("\\s+");
dCanons. add (pair [0]);
dWords. add (pair [1]);
}
BufferedReader dfile = new BufferedReader( new FileReader(args[0]) );
ArrayList
jWord.add("");
while ( jWord.ready() )
{
String secLine = jWord.readLine();
jWord.add(secLine);
}
jWord.close();
Collections.sort(jWord);
for (String line: jWord)
{
System.out.print(jWord + " ");
jCanon = toCanonical(jWord);
index = Collections.binarySearch(dCanons, jCanon);
if (index < 0)
{
System.out.println();
}
else
{
for ()
{
if (dCanons.equals(jCanon))
System.out.print (dWords + " ");
else
System.out.println();
}
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
