How to get input using the Scanner Program in Java

Here is a program with explaination on How to get input using the Scanner Program in Java?

import java.util.Scanner;
class GetInputFromUser
{
public static void main(String args[])
{
int a; 
float b; 
String s;

Scanner in = new Scanner(System.in);

System.out.println("Enter a string"); 
s = in.nextLine();
System.out.println("Your entered string "+s);

System.out.println("Enter an integer"); 
a = in.nextInt();
System.out.println("Your entered integer "+a);

System.out.println("Enter a float"); 
b = in.nextFloat();
System.out.println("You entered float "+b);
}
}

Let’s break down the given Java program step by step:

Step-by-step Explanation of How to get input using the Scanner Program in Java:

 1. import java.util.Scanner;

This line imports the Scanner class from the java.util package. The Scanner class is used to read input from the user (keyboard).

 2. class GetInputFromUser

This defines a class named GetInputFromUser. The program’s logic will be written in this class.

3. public static void main(String args[])

This is the main method. It is an entry point in any Java program and execution begins here.

  • public: That’s it, it is a public method; may be accessed everywhere in the system. Also has to be called from outside; hence it should be called whenever this application invokes using JVM.
  • static: This method is declared static and, hence can be invoked by a class. An instance need not be created of that class.
  • void: The method doesn’t return any value.
  • String[] args: These could be some array of strings which might potentially be command-line arguments to this program that the runtime passes, so the compiler must declare its presence, it just does nothing with the given array as part of the main in any subclass.

4. Variable declaration:

  • int a; declare an integer variable ‘a’ for inputting the integer
  • float b; declare a floating point variable ‘b’ to float inputs
  • String s; This declares a string variable ‘s’ to store the string input

 5. Scanner in = new Scanner(System.in);

  • This is created Scanner class object named in that will be used to read input from the user using the console (keyboard).
  • System.in is a standard input stream that reads from the keyboard, as follows:

6. System.out.println(“Enter a string”);

The statement displays the message “Enter a string” on the console. It asks for the user’s input of a string.

 7. s = in.nextLine();

  • This statement reads in a whole line of text from the keyboard for the user to input and places it into the variable s. 
  • The nextLine() method reads input until the user enters the Enter key.

 8. System.out.println(“You entered string ” + s);

This prints the user input string entered preceding it with message  “You entered string “.

 9. System.out.println(“Enter an integer”);

Outputs the message in the following – “Enter an integer”. Now it asks a user to read an integer.

10. a = in.nextInt();

This statement reads an integer from the keyboard from the user and stores it into the variable a. The method nextInt() reads the next integer from the keyboard.

11. System.out.println(“You entered integer ” + a);

This prints the integer entered by the user, prefixed with the message “You entered integer .

12. System.out.println(“Enter a float”);

This statement prints the message  “Enter a float”, asking the user to enter a floating-point number.

 13. b = in.nextFloat();

Reads a floating-point input from the user and assigns it to the variable b. The method nextFloat() reads the next float input.

14. System.out.println(“You entered float ” + b);

Prints the float number that has been entered by the user with a prefix message “You entered float “.

15. Closing curly braces }

The closing curly braces denote the end of the main method and the GetInputFromUser class.

Example of Program Execution:

Suppose the user responds as follows when prompted:

1. String: "Hello, Java!"
2. Integer: 25
3. Float: 3.14

Output

Enter a string
Hello, Java!
Your entered string Hello, Java!

Enter an integer
25
Your entered integer 25

Enter a float
3.14
Your entered float 3.14

In this example, the program first prompts the user for a string followed by an integer and a float. All of the entries are printed back with their respective messages.

Leave a Comment

Your email address will not be published. Required fields are marked *