In: Computer Science
Create a Java application that will prompt the user for the first name of 6 friends in any order and store them in an array. First, output the array unsorted. Next, sort the array of friends and then output the sorted array to the screen. Be sure to clearly label the output. See the example program input and output shown below. It does not have to be exactly as shown in the example, however it gives you an idea of how to structure the program.
import java.util.*;
public class Main
{
public static void main(String[] args) {
String tmp;
Scanner s = new Scanner(System.in);
System.out.print("Enter 6 names :");
String arrayName[] = new String[6];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 6; i++)
{
arrayName[i] = sc.nextLine();
}
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 6; j++)
{
if (arrayName[i].compareTo(arrayName[j])>0)
{
tmp = arrayName[i];
arrayName[i] = arrayName[j];
arrayName[j] = tmp;
}
}
}
System.out.print("\nNames in Sorted Order: ");
for (int i = 0; i < 5; i++)
{
System.out.print(arrayName[i] + ",");
}
System.out.print(arrayName[5]);
}
}
DON'T FORGET TO HIT LIKE.
THANKS BY HEART
COMMENT IF ANY PROBLEM.