WRITE A PROBLEM TAKE 20 FRIENDS NAME IN AN ARRAY (FRIEND NAME CONSISTS OF NAME AND SURNAME ) AND ARRAGE THEM IN ASCENDING ORDER OF THEIR SURNAME
Answers
Answer:
Quote for Quote from the text: "Write a program that asks the user to enter three names, and then displays the names sorted in ascending order. For example, if the user entered "Charlie","Leslie, and "Andy", the program would display
Andy
Charlie
Leslie
My professor specifically said we are not allowed to use loops or arrays since we have not covered that in class. I have been trying to use the compareTo method but cant seem to get it to run with more than two string variables.
public class SortedNames {
public static void main(String[] args) {
//Declare Variables
String name1;
String name2;
String name3;
//Accept User Imput
Scanner keyboard=new Scanner(System.in);
System.out.print("Please Enter First Name ");
name1=keyboard.nextLine();
System.out.print("Please Enter Second Name ");
name2=keyboard.nextLine();
System.out.print("Please Enter Third Name ");
name3=keyboard.nextLine();
//Compare
if((name2.compareToIgnoreCase(name1)<0)&&(name2.compareToIgnoreCase(name3)<0))
{
System.out.println(name2);
}
//Compare
if((name1.compareToIgnoreCase(name2)<0)&&(name1.compareToIgnoreCase(name3)<0))
{
System.out.println(name1);
}
//Compare
if((name3.compareToIgnoreCase(name1)<0)&&(name3.compareToIgnoreCase(name2)<0))
{
System.out.println(name3);
}
}
}