Pages

Read a word user input in java

Complete source code below, will show you how to wait and read only a word from user input in command prompt.

*****************************************************************
COMPLETE SOURCE CODE FOR : ReadOneWordUserInput.java
*****************************************************************


import java.util.Scanner;

public class ReadOneWordUserInput
{
public static void main(String[]args)
{
//Create a scanner object
//Scanner is a predefined class in java that will be use to scan text
//System.in is mean, we will receive input from standard input stream
Scanner readUserInput=new Scanner(System.in);

//Print into command prompt(What is your gender ?)
//Print into command prompt(Female/Male)
System.out.println("What is your gender ?\nFemale/Male");

//Wait and READ A WORD from user input after user hit 'Enter'
//Method next() in class scanner will read only first word in user input
//So, second word that seperate by space will be ignore.
String myGender=readUserInput.next();

//Print what store in myGender
System.out.println(myGender);
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************
Related Posts Plugin for WordPress, Blogger...