Pages

Wednesday, October 7, 2015

Java most beginner tutorial use of scanner object..how to get input from user

Assalam-o-alaikum !

JAVA MOST BEGINNER PROGRAMMING

   how to take input from user using scanner


package scanner;

import java.util.Scanner;

/*scanner import belong to util library*/

public class example{

/*keep in mind you can declare the variable in the main function as well as out of main ...if you wanna make it out from the main so you just put static from the scanner object like this*/

Static Scanner input = new Scanner(System.in);
/* what we did up here is first we created a scanner object.its  out of main function so we made it static ...static mean we can access it from any where...and as a parameter it take System.in ..so you should put that.because you wanna input from the user .*/

/*and now import your scanner you can import it by typing Ctr+shift+o from your keyboard or just hover your mouse on scanner and it will automatically pop up scanner import for you..and you just press on it*/


public static void main (String[]args){

System.out.println("what's your name?");

/*It's mean you gave an output to the user.you asked his name.now this is the time to use Scanner object.so we have given our Scanner name input .now give users a line to type something in it like this
*/
input.nextLine();

/* it's mean user can type text in the next line,and right now if you want to store the user input.so you can do it.Just create a String variable.like this*/
String storeUserInput;
storeUserInput = input.nextLine();
/*so now if anything a user would type ..it will store into our storeUserInput variable ,now if you want to show them their text back.do like this*/

System.out.println(storeUserInput);



 }


}
all your activities will look like this


and now if you run this it would ask about your name if you enter your name so it will then look like this



i hope you got the idea if you wanna watch tutorial about this visit me on


to catch more of me like subscribe and share with your friends

No comments:

Post a Comment