BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to get input from user

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial we are gonna see how to accept input from user. We are using Scanner class to get the input. In the below example we are getting input String, integer and a float number. For this we are using following methods:
1) public String nextLine(): For getting input String
2) public int nextInt(): For integer input
3) public float nextFloat(): For float input

Example:

import java.util.Scanner;

class GetInputData
{
  public static void main(String args[])
  {
     int num;
     float fnum;
     String str;
 
     Scanner in = new Scanner(System.in);
 
     //Get input String
     System.out.println("Enter a string: ");
     str = in.nextLine();
     System.out.println("Input String is: "+str);
 
     //Get input Integer
     System.out.println("Enter an integer: ");
     num = in.nextInt();
     System.out.println("Input Integer is: "+num);
 
     //Get input float number
     System.out.println("Enter a float number: ");
     fnum = in.nextFloat();
     System.out.println("Input Float number is: "+fnum); 
  }
}

Output:

Enter a string: 
Chaitanya
Input String is: Chaitanya
Enter an integer: 
27
Input Integer is: 27
Enter a float number: 
12.56
Input Float number is: 12.56

Reference:
Scanner Javadoc

Top Related Articles:

  1. java program to find factorial of a given number using recursion
  2. Neon Number in Java with example
  3. Sunny Number Program in Java
  4. Peterson Number Program in Java
  5. Automorphic Number Program in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. jocelyn says

    September 11, 2017 at 8:59 PM

    hi,
    why i use scanner.nextLine(), it doesnt seem to allow me to input the value but automatically read the space as the string thus print it out…
    could you please explain? thanks!

    System.out.println(“input the String:”);
    str = scanner.nextLine();
    System.out.println(“the String you input is: ” + str);

    Reply
    • Jafet says

      September 24, 2017 at 10:10 AM

      It will not work that way. You have to use “in” instead of scanner. e.g Scanner in = new Scanner(System.in);

      Therefore it will be like this to work:
      System.out.println(“input the String: “);
      str = in.next();
      System.out.println( ” the String you input is: ” + str);

      Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap