How to take input as String with spaces in java using scanner

Your code work fine. I just add little modification:

package practise;

import java.util.Scanner;

public class scanccls {

    public static void main(String[] args) {

        System.out.println("Enter your name:"); 
        Scanner scan = new Scanner(System.in);
        String name="";

        name+=scan.nextLine();
        scan.close();

        System.out.println("Your name is :"+name); 

    }

}

Leave a Comment