Question: Please help me write this java program. The prompt is: the program should take two command args: a dictionary file and a jumbles file. For

Please help me write this java program. The prompt is: the program should take two command args: a dictionary file and a jumbles file. For each jumbled word you must find all the matching dictionary words and print them after the jumbled word ion the line.

We have to write a binary search algorithm by hand and that is the part I am having trouble with. If somebody could just help with the binary search algorithm that would be great. The binary search algorithm must use the method .startswith() to find what it is searching for. Below is the code I have with the binary search part near the bottom.

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

public class Jumbles { public static void main( String args[] ) throws Exception { //int count = 0; //int count2 = 0; if (args.length < 2) { System.out.println("Please include a dictionary and a jumbles file."); System.exit(0); } ArrayList dictionary = new ArrayList(); ArrayList jumbles = new ArrayList(); ArrayList answerKey = new ArrayList(); BufferedReader dictionaryInFile = new BufferedReader( new FileReader(args[0]) ); BufferedReader jumblesInFile = new BufferedReader( new FileReader(args[1]) ); while ( dictionaryInFile.ready() ) { dictionary.add( dictionaryInFile.readLine() ); //System.out.println( dictionary.get(count) ); //count++; } dictionaryInFile.close(); while ( jumblesInFile.ready() ) { jumbles.add( jumblesInFile.readLine() ); //System.out.println( jumbles.get(count2) ); //count2++; } jumblesInFile.close(); //System.out.println("Count1 is: " + count); //System.out.println("Count2 is: " + count2); String test = "bell"; Collections.sort( dictionary ); Collections.sort( jumbles ); for (int i = 0; i < dictionary.size() ; i++) { String dictionaryWord = dictionary.get(i); String dCanon = toCanon( dictionaryWord ); int index = bSearch( answerKey, dCanon ); if ( index > 0 ) { answerKey.set( index, answerKey.get(index) + "" + dictionaryWord ); System.out.println( answerKey.get(index) ); } else { index = -(index + 1); answerKey.add( index, dCanon + "" + dictionaryWord); System.out.println( answerKey.get(index) ); } } //for (int j = 0; j < jumbles.size(); j++) //{ //} } static String toCanon( String word ) { char[] letters = word.toCharArray(); Arrays.sort( letters ); return new String( letters ); } static int bSearch( ArrayList answerKey, String canonPrefix ) { int start = 0; int end = answerKey.size() + 1; int mid; while (start <= end) { mid = start + (end - start) / 2; if ( answerKey.get(mid).startsWith( canonPrefix ) == true ) { return mid; } else if ( answerKey.get(mid).startsWith( canonPrefix ) ) { start = mid + 1; } else { end = mid - 1; } } int indexWhereItBelongs = (start + 1) * (-1); return indexWhereItBelongs; } }

Here is the dictionary input file:

act cat tac dog god post pots stop spot tops opts rat tar art trap tarp part flog golf frog gorp glossy 

Here is the jumbles file:

atc otsp gdo atr arpt grof sylogs 

Here is the correct output:

arpt part tarp trap atc act cat tac atr art rat tar gdo dog god grof frog otsp opts post pots spot stop tops sylogs glossy 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!