
a java program that will exchange the last names of two students that were input by a user

import java.util.*;
class ReplaceLastNames{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter Name1: ");
String name1=input.nextLine();
System.out.print("Enter Name2: ");
String name2=input.nextLine();
String st1[]=name1.split(" ");
String st2[]=name2.split(" ");
String f1=st1[0];
String l1=st1[1];
String f2=st2[0];
String l2=st2[1];
System.out.println("Names after replacing the last names: ");
String n1=f1+" "+l2;
String n2=f2+" "+l1;
System.out.println(n1);
System.out.println(n2);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.