Saturday, January 21, 2017

Learn Java for Beginner easily ! part 5 : User Input

So when you're making a program.There will be a user who use the program, They are called the user (Human).The user use your program by doing an input of data that your program ask.In this part of tutorial I'm gonna teach you guys how to get user input in Java. Let's get start and pay attention !

First, I want you guys to type this program.Then I want you guys to Run the program.



This program will ask you to input a text in the console box below.
So in this case, I will type "Hey Blogger ! ".






After that Press 'Enter'. Then there should be an output like this.


Here is the explanation of the codes on the picture above :

Line 1 :


"Import java.util.Scanner;" is a keyword for you to tell Java that you need to use the scanner utility. So, whenever you want to input some stuff in your program when it runs, you will have to import the java scanner's utility by typing "Import java.util.Scanner;" in your program.

Line 2 & 3 :

I have explained it in Part 2. Go check it out if you still confuse about it.

Line 5 :



The code on the picture above is the declaration of variable "cherry" as a place where your Input stored. System.in  shows that whatever you type on your keyboard to input data, the data that you input will be stored in the "cherry" variable.

Line 7 :


The code above is to tell java to print out the text inside the "cherry" variable.

Q : Why do I have to type ".nextLine" after the variable's name ?

A : Because that's how you tell Java that you want the input as a string(sentences). If you want your input as an integer number only, you're gonna have to type ".nextInt()"  after your variable's name.

So that's it, if you are confuse, read it repeatedly and carefully and don't forget to use that brains. I ain't gonna explain from A to Z like your teacher does at your school guys. The reason why I always show the program in picture is because I want you guys to type it, so that if you are typing it often enough, Your hands will get used to it.If you guys have any question. Don't be hesitate to ask me on the comment column below.

























No comments:

Post a Comment