This section shows how to get user input. We use the scanner class to get user input. The following example code shows how to get the string, integer, and float data entered by the user. The main use of the following methods:
1) public string nextline (): Gets the input string
2) public int nextint (): Gets the integer entered
3) public float nextfloat (): Gets the input float number
Import java.util.scanner;class getinputdata{public static void Main (String args[]) {intnum; floatFnum; String str; Scannerinch= new Scanner (System.inch); //Get Input StringSystem.out.println ("Enter A string:"); STR=inch. nextline (); System.out.println ("Input String is:"+str); //Get Input IntegerSystem.out.println ("Enter an integer:"); Num=inch. Nextint (); System.out.println ("Input Integer is:"+num); //Get Input Float numberSystem.out.println ("Enter A float number:"); Fnum=inch. Nextfloat (); System.out.println ("Input Float number is:"+fnum); }}
Getting Started with Java: Getting user input for the underlying algorithm