Java Program to Print the given word

Get a Word from user and display the same.

Sample Input 1:

hello

Sample Output 1:

hello

Sample Input 2:

hi boss

Sample Output 2:

hi

Program or Solution

				
			
					
import java.util.*;
class Word
{
 
   public static void main(String args[])
   {
 
	String Input1;
	Scanner sc=new Scanner(System.in);
	
	System.out.println("Get a Word from user and display the same.");
	
	System.out.println("Enter The Input 1:");
	Input1=sc.next();
	System.out.println(Input1);
	
	

  }

}
			
				
			

Output

Java Program to Print the given word Output

Program Explanation

The java.util.Scanner.next() method finds and returns the scanner's next complete token. So it just takes the word. next() do not consider the characters after the spaces. Try with the Input "Hello World", you will get "Hello".