Hi. I already have a java program made that prompts the user to enter a file nam
ID: 3635097 • Letter: H
Question
Hi. I already have a java program made that prompts the user to enter a file name. After doing this, my program already knows how to read the file that the user enters. The file that the user enters is file that contains different people names.I know how to alphabetize a string, but my problem is I do not know how to alphabetize something from a file.
In other words, if I am given that I have to sort three letters, I can do that:
String[] strArray = new String[] {"z", "a", "C"};
Arrays.sort(strArray);
BUT I do not know how to sort something like this:
String[] names = new String[20]
This is my thinking on how to solve this issue, but I'm not sure if I am correct:
so i have in my main:
String[] names = new String[20]
ascendingSort(names, names.length);
now i am going to create another method:
public static void ascendingSort(String[] names, double length)
?
Please help, I'll rate
Explanation / Answer
Dear,
Here is the method definition for your post...
public static void ascendingSort(String[] names, double length) {
String temp;//to store temporary data
//outer loop
for (int i = 0; i < length - 1; i++) {
//inner loop
for (int j = 0; j < length - 1; j++) {
//comparing adjacent strings
if (names[j].compareToIgnoreCase(names[j + 1]) > 0)
{
//if condition satisfies swap two strings
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}//end of inner loop
}//end of outer loop
}//end of method
Hope this helps you....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.