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

前端 未结 7 1792
春和景丽
春和景丽 2021-02-02 04:29

I need to read spaces (present before string and after String) given as input using Scanner Note : if there is no spaces given in input it should not add space in output

7条回答
  •  感情败类
    2021-02-02 04:57

    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();
    
            // Can also be done like
            // String name=scan.next();
            // name+=scan.nextLine();
    
            // They Both Work as same
    
            System.out.println("Your name is :" + name);
        }
    }
    

提交回复
热议问题