Friday, 13 February 2015

Prompting User For Input.


                                  How To Prompt User For Input??

1. Code :
    import java.util.Scanner; 

class Input{
   
   
   public static void main(String[] args){

   Scanner in = new Scanner(System.in);
     
System.out.println("Enter A String");
String str = in.nextLine();
 
   System.out.println("Enter A Number");
   int num = in.nextInt();
   
   
      System.out.println("Enter A Float Number");
      Float flt = in.nextFloat();
   
    System.out.println("Number is: "+str);
System.out.println("String is: "+num);
 System.out.println("Float Value is: "+flt);
   }
}

2.Scanner is class that helps to read/fetch the user's input.

3.Create reference ("in") to class Scanner to take the input.
   Example: Scanner in = new Scanner(System.in);
    --it can be anything of your choice.

4.import Scanner package from java.util.Scanner;

5.Take String Input at first, then followed by others.

6. nextLine for String input.
   nextFloat for Float input.
   nextInt for int input and so on.

7. ScreenShot with Steps to run:

No comments:

Post a Comment