Question
Write a program in Java which accepts an input text le containing words, saves it to an array, sorts the array according to the rst
Write a program in Java which accepts an input text le containing words, saves it to an array, sorts the array according to the rst letter and prints the sorted array to the console. Assume that the words are composed of letters (upper/lower case) and digits only. In order to sort, you must use Insertion Sort (no other type of sorting is permitted). It is assumed that two adjacent words in the input text le are separated just by a comma. The name of the Java class must be StringSorter with a method sortStrings() {...}. The method must print out the following message if a word is found which does not start with an uppercase letter: Invalid input. Word found which does not start with an uppercase letter. To test if a character is an uppercase letter or not, you may use the following method: Character.isUpperCase();
A test file is given named TestStringSorter.java
It looks like this;
public class TestStringSorter { public static void main(String[] args) { StringSorter sorterA = new StringSorter("myFile.txt"); sorterA.sortStrings(); StringSorter sorterB = new StringSorter("mySecondFile.txt"); sorterB.sortStrings(); StringSorter sorterC = new StringSorter("myThirdFile.txt"); sorterC.sortStrings(); }
}
the other file (StringSorter.java) looks like this;
public class StringSorter { private String inputFileName;
public StringSorter(String fileName) { inputFileName = fileName; } public void sortStrings() {
//this is the method to fill out } }
My problem is that I know how to use insertion sort in order to sort the string, but im not too sure what the first few lines of StringSorter do. I know that they have to deal with the taking in of the files, but I don't know how to use those files or if the sortStrings method should be taking in a string. Please reply with code that has comments so I can follow. thank you.
+below is what I have so far but I know its broken, just as a side note can you tell me if I was on the right track;
public void sortStrings(String myFile[] ) { int L = myFile.length; int i, j; String temp; for (i = 1; i < L; i++) { j=i; temp = myFile[i]; while (j > 0 && temp.charAt(0) < myFile[j-1].charAt(0)) { myFile[j] = myFile[j-1]; j = j-1; } myFile[j]= temp; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started