GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — Input in Java Programming | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Java Programming Tutorial
  • Java Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Integer Variable
    • Long Variable
    • Short Variable
    • Byte Variable
    • Float and Double Variables
    • Character Variable
    • Boolean Variable
    • Constant Variable
    • Typecasting
  • Operators in Java
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Operator Precedence
  • Input and Output in Java
    • Environment Setup
    • Program Basic Structure
    • Output
    • Input
    • Comments
  • Decision Making in Java
    • Decision Making
    • if Statement
    • if else Statement
    • if else if Statement
    • Conditional Operator
    • switch case Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Java
    • Loops
    • for Loop
    • while Loop
    • do while Loop
  • Arrays in Java
    • One Dimensional Array
    • Two Dimensional Array
  • String in Java
    • String
    • StringBuffer
    • StringBuilder
  • Searching in Java
    • Linear Search
    • Binary Search
  • Sorting in Java
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Java
    • Function
    • Recursive Function
    • Function Overloading
  • OOPs in Java
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Access Modifiers
    • Constructor
    • Static Variables
    • Static Methods
    • Inheritance
    • Interface
    • Types of Inheritance
    • Polymorphism
    • Final Class and Method
    • Abstract Class and Method
    • Wrapper Classes
  • Data Structures in Java
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Packages in Java
    • Packages
  • Exception Handling in Java
    • Exception Handling
    • User-Defined Exception
  • Thread in Java
    • Thread
    • Thread Methods
  • File Handling in Java
    • Text File Handling
    • Binary File Handling
    • Random Access File Handling

bookmark
Table of Contents

    Input in Java Programming

    In this lesson, we will understand what is Scanner class, and how it is used in the Java program to accept input with the help of some examples.

    Input in Java

    The Scanner class is used in Java programs to accept input from the standard input device (keyboard) and store it in one or more variables. The Scanner class is available in the java.util package.

    Video Explanation

    Click the button below to watch the explanation.

    Please wait while the video is loading....
    video image

    To use the Scanner class, we need to create an object of the Scanner class and use the available methods in the class to take different types of input from the user. The available methods in the Scanner class are listed below.

    Method Description
    nextBoolean() Reads a boolean type value from the user
    nextByte() Reads a byte type value from the user
    nextDouble() Reads a double type value from the user
    nextFloat() Reads a float type value from the user
    nextInt() Reads an int type value from the user
    nextLong() Reads a long type value from the user
    nextShort() Reads a short type value from the user
    nextLine() Reads a String type value from the user

    Let's see an example of how to use Scanner class to read different types of data from the keyboard.

    Example
    import java.util.Scanner;  // Import the Scanner class
    
    public class Example
    {
        public static void main(String[] args)
        {
            int age;
            float height;
            double weight;
            char initial;
    
            // Create a Scanner object to read input
            Scanner sc = new Scanner(System.in);
    
            // Ask the user to enter their age
            System.out.print("Enter your age: ");
            age = sc.nextInt();
    
            // Ask the user to enter their height
            System.out.print("Enter your height in meters: ");
            height = sc.nextFloat();
    
            // Ask the user to enter their weight
            System.out.print("Enter your weight in kilogram: ");
            weight = sc.nextDouble();
    
            // Consume the newline character left by the sc.nextDouble()
            sc.nextLine();  // This fixes the issue
    
            // Ask the user to enter the first character of their name
            System.out.print("Enter the first character of your name: ");
            initial = sc.nextLine().charAt(0);	// Reads the first character from input
    
            // Print the values entered by the user
            System.out.println("\nYou entered:");
            System.out.println("Age: " + age);
            System.out.println("Height: " + height);
            System.out.println("Weight: " + weight);
            System.out.println("Initial: " + initial);
        }
    }
    
    Output
    Enter your age: 15
    Enter your height in meters: 3.423
    Enter your weight in kilogram: 45.21543
    Enter the first character of your name: R
    
    You entered:
    Age: 15
    Height: 3.423
    Weight: 45.21543
    Initial: R                               
    What Happens in the Example
    1. Prompting the User:
    System.out.print is used to ask the user to enter their age, height, weight, and initial.
    2. Reading Input:
    age = sc.nextInt(); reads an integer type number input by the user and stores it in the variable age.
    height = sc.nextFloat(); reads a float type number input by the user and stores it in the variable height.
    weight = sc.nextDouble(); reads a double type number input by the user and stores it in the variable weight.
    sc.nextLine() consume the newline character left by the sc.nextDouble().
    initial = sc.nextLine().charAt(0); reads the first character from the user input and stores it in the variable initial.
    3. Displaying the Input:
    System.out.println() shows the values entered by the user, confirming they were read correctly.

    Test Your Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy.

    Cookie Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    Mentora Question Asked: 0 / 20

    Hi, I’m Mentora!

    I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way.

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.