The main method handles all the user input and all the println statements. It as
ID: 3687195 • Letter: T
Question
The main method handles all the user input and all the println statements. It asks the user to his or her first and last name all on one line. The main method should call the changeNameFormat method described below and print out the results from that method. The method changeNameFormat takes a String which is a name like this John Smith and returns a String which is the name like this Smith, John The output should look similar to this…. Enter your name: John Smith Your name for our system will be Smith, John.
Explanation / Answer
NameFormat.java
public class NameFormat {
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Enter your name:");
String name = in.nextLine();
String newName = changeNameFormat(name);
System.out.println("Your name for our system will be "+newName);
}
public static String changeNameFormat (String name){
String names[] = name.split(" ");
String firstName = names[1];
String lastName = names[0];
return firstName +", "+ lastName;
}
}
Output:
Enter your name:
John Smith
Your name for our system will be Smith, John
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.