Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Read all of the lines from the provided file dictionary.txt and store them in an

ID: 3659053 • Letter: R

Question

Read all of the lines from the provided file dictionary.txt and store them in an array of Strings. You can hardcode the input filename and you can take advantage of the fact that there are exactly 9992 lines in the file (this is a spellcheck dictionary file so I don't take any responsibility for any questionable or offensive words contained within). The idea here is to create an array that is the right size to hold each of the words (one per line in the file) and then, in a loop add them to the array - first word in array [0], next in array [1] ... and so on. Modify the selection sort method from the book (or slides) so that it will sort an array of Strings in alphabetical order. Your method should have the header: public static void selectionSort (String[] array) Your sort should be case-insensitive so that upper and lower case words are considered equal for the sorting. Call this method in main { } to sort the array of Strings that you read in from the file. To show that it is sorted display only the first 5 and last 5 in the array as there are too many to print to the screen. Write a linear search method with the following header: public static int linearSearch (String[] array. String s) that returns the index (position) of String s in the array if it is found and -1 if it is not found. The method should return the first occurrence of the string if it occurs multiple times in the array. Show that your search method is working by prompting the user to enter in strings to find. Loop the search request until the user enters the empty string

Explanation / Answer

package Cramster; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class question2 { public static void main(String[] args) { String fileName = "dictionary.txt"; int linesInFile = 9992; try { Scanner inputFile = new Scanner(new File(fileName)); String[] array = new String[linesInFile]; int i = 0; while(inputFile.hasNext()) { array[i++] = inputFile.nextLine(); } inputFile.close(); if(i != linesInFile) throw new IllegalArgumentException(); selectionSort(array); Scanner console = new Scanner(System.in); System.out.printf("Type word to perform Linear Searches. " + "Enter Empty String ("") to terminate input."); while(true) { System.out.printf("Enter a Word: "); String s = console.next(); if(s.equals("""")) { System.out.printf("You Entered an Empty String." + "Terminating Input "); break; } int result = linearSearch(array,s); System.out.printf("%s ",result == -1 ? String.format("Word %s is not found in the dictionary. ",s) : String.format("Word %s is found at location %d ",s,result +1)); } } catch(IllegalArgumentException e) { System.err.printf("Error! "%s" file doesn't have %d lines." + "Terminating Program ",fileName,linesInFile); System.exit(1); } catch (FileNotFoundException e) { System.err.printf("File %s not Found. Terminating Program",fileName); } } public static void selectionSort(String[] array) { int smallest; for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote