I am trying to take input for two strings. One is for first name without any spaces and second one is for fullname that will include spaces between first-name and the last-name. As fname variable only holds one word so I used next() function there and nextLine() for full_name variable. But the code only takes input for fname and then terminates without taking input for full name. The IDE shows no error.
import java.util.Scanner;
public class main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter your first name: ");
String fname = sc.next();
System.out.println(fname);
System.out.print("Enter your full-name: ");
String full_name = sc.nextLine();
System.out.println(full_name);
}
}
>Solution :
A quick fix for your problem would be to change
this
String fname = sc.next();
into this
String fname = sc.nextLine().split(" ")[0];