Input to the variable


Java Scanner

Java Scanner is used to get the input from the user in java programming language.
In this tutorial, we are going to study about the Java Scanner.
Let`s create a file with name JavaScanner.java.
We need to first import scanner utility in java.
Syntax for importing Scanner
import java.util.Scanner
In java we create classes. Java classes are blueprints or maps from which we create objects in the computer memory.
Lets create a class named JavaScanner. Here is the syntax.
public class JavaScanner {}. Now we write method body inside the curly brackets.
A Java method is a list of tasks which needs to be done.
Syntax for java main method:
To run a java program a "main" method is required. A method named "main" should be present in the class that is going to be starting point of the Java application. So, lets create a method named "main".

public static void main (String args[]) {}
To use the Scanner utility, we need to create an object. To create an Object in java we use the following syntax.
Scanner Scan = new Scanner();
To get user input the above we need to use the system input stream
Scanner Scan = new Scanner(System.in);
The user input will be printed to the screen using the below syntax.
System.out.println(Scan.nextLine());
So Let`s open the file we created named "JavaScanner.java" and add the following lines as shown below.
 /**
    * JavaToHtmlTest/Java2HtmlTest is a test program to display
    * capabilites of togotutor's online tool for converting Java Code to
    * HTML format. The generated HTML code can be pasted on any web page
    * to display properly formatted Java Code.
    */


import java.util.Scanner;

public class JavaScanner {

    public static void main(String args[]) {

        Scanner Scan = new Scanner(System.in);
        System.out.println(Scan.nextLine());

    }
}





















Now we will compile the java program using javac command, as javac will convert the program "JavaScanner.java" to "JavaScanner.class" which is a machine understandable class file.
Go to Start Menu --> Run cmd (for command prompt) --> type javac JavaScanner.java.
Once compiled successfully, without errors, you will now see a file created with the name "JavaScanner.class".
Go to Start Menu --> Run cmd (for command prompt) --> type java JavaScanner. Here we do not need any file extenstion.



I type Hello togo, when prompt
Output
Hello togo