Write a java code that allows the user to input any name (up to 10 only) and then sorts and alphabetizes them in an array without using predefined methods (.compareTo(), or any sorter functions) and then print out the results.
code
import java.util.*;
public class Main
{
public static void main(String[] args)
{Scanner s=new Scanner(System.in);
//input
String[] name = new String[10];
for(int i=0;i<10;i++)
name[i]=s.nextLine();
int size = name.length;
for(int i = 0; i < size - 1; i++)
{
for(int j = i + 1; j < name.length; j++)
{
//sort the words
if(name[i].charAt(0)>name[j].charAt(0))
{
String temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
//result
System.out.println("After sorting : ");
for(int i=0;i<size;i++)
System.out.print(name[i]+",");
}
}
//screenshot of the code
output:
--------------------------------------------
Get Answers For Free
Most questions answered within 1 hours.