Write a Java program with String class that will sort first, middle and last nam
ID: 3586725 • Letter: W
Question
Write a Java program with String class that will sort first, middle and last names of the user. The program will ask user to enter the first, middle and the last names. It will also ask the sorting order e.g. you will enter ‘1’ for ascending order and ‘2’ for descending order. The program should display the three names arranged in the specified sort order of their lengths.
For example, if you enter name as:
First name: Pradeep
Middle name: K.
Last name: Atrey
Sort order: 1
As you can see the length of (or number of characters in) the first name is 7, middle name is 2 and last name is 5; and if you choose to ‘1’ for ascending order, the output would be:
K. Atrey Pradeep
i.e. first, display the name with the least number of characters, and so on.
For documentation, I don’t expect you to add comment in each line, but you must explain the code appropriately.
Explanation / Answer
NameSort.java
import java.util.Scanner;
public class NameSort {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("First name: ");
String firstName = scan.next();
System.out.print("Middle name: ");
String middleName = scan.next();
System.out.print("Last name: ");
String lastName = scan.next();
System.out
.println("Enter your choice: 1 for ascending and 2 for desencing. ");
int choice = scan.nextInt();
if (choice == 2) {
if (middleName.length() > firstName.length()
&& middleName.length() > lastName.length()) {
System.out.print(middleName + " ");
if (firstName.length() > lastName.length()) {
System.out.print(lastName+ " ");
System.out.print(firstName+ " ");
} else {
System.out.print(firstName+ " ");
System.out.print(lastName+ " ");
}
} else if (firstName.length() > middleName.length()
&& firstName.length() > lastName.length()) {
System.out.print(firstName + " ");
if (middleName.length() > lastName.length()) {
System.out.print(middleName+ " ");
System.out.print(lastName+ " ");
} else {
System.out.print(lastName+ " ");
System.out.print(middleName+ " ");
}
} else {
System.out.print(lastName + " ");
if (middleName.length() > firstName.length()) {
System.out.print(middleName+ " ");
System.out.print(firstName);
} else {
System.out.print(firstName+ " ");
System.out.print(middleName+ " ");
}
}
} else {
if (middleName.length() < firstName.length()
&& middleName.length() < lastName.length()) {
System.out.print(middleName + " ");
if (firstName.length() < lastName.length()) {
System.out.print(firstName+ " ");
System.out.print(lastName+ " ");
} else {
System.out.print(lastName+ " ");
System.out.print(firstName+ " ");
}
} else if (firstName.length() < middleName.length()
&& firstName.length() < lastName.length()) {
System.out.print(firstName + " ");
if (middleName.length() < lastName.length()) {
System.out.print(middleName+ " ");
System.out.print(lastName+ " ");
} else {
System.out.print(lastName+ " ");
System.out.print(middleName+ " ");
}
} else {
System.out.print(lastName + " ");
if (middleName.length() < firstName.length()) {
System.out.print(middleName+ " ");
System.out.print(firstName+ " ");
} else {
System.out.print(firstName+ " ");
System.out.print(middleName+ " ");
}
}
}
}
}
Output:
First name: Pradeep
Middle name: K.
Last name: Atrey
Enter your choice: 1 for ascending and 2 for desencing.
1
K. Atrey Pradeep
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.