Answer:
Complete definition of method processName is as follows:
public static void processName(Scanner in){
System.out.println("Please enter your full name: ");
String Name = in.nextLine();
String [] strings = Name.split(" ");
String firstName = strings[0];
String lastName = strings[1];
System.out.println("Your name in reverse order is " + lastName+ " "+ firstName);
}
Step-by-step explanation:
Here subString method is not useful to break the string into first name and last name.
The better idea is to use the spilt(); function of string. split() function is used to split the given string into two halves depending on the delimiter passed as argument.
In the above method blank space (" ") is passed as delimiter. Thus split function stores the first half of string before blank space in strings array at location 0 and 1 respectively.
Sample program to implement above method:
import java.util.Scanner;
public class Main{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
processName(in);
}
public static void processName(Scanner in){
System.out.println("Please enter your full name: ");
String Name = in.nextLine();
String [] strings = Name.split(" ");
String firstName = strings[0];
String lastName = strings[1];
System.out.println("Your name in reverse order is " + lastName+ " "+ firstName);
}
}
Sample output:
Please enter your full name:
Jim hoppers
Your name in reverse order is hoppers Jim