Java Tutorial: Getting User Input in Java

 

Java Tutorial: Getting User Input in Java

Reading data from the Keyboard :

  • Scanner class of java.util package is used to take input from the user's keyboard.The Scanner class has many methods for taking input from the user depending upon the type of input. To use any of the methods of the Scanner class, first, we need to create an object of the Scanner class as shown in the below example :
    import java.util.Scanner;  // Importing  the Scanner class
    Scanner sc = new Scanner(System.in);  //Creating an object named "sc" of the Scanner class.

    Taking an integer input from the keyboard :

    Scanner S = new Scanner(System.in);  //(Read from the keyboard)
    int a = S.nextInt();  //(Method to read from the keyboard)


Code:

package com.company;
import java.util.Scanner;

public class CWH_05_TakingInpu {
    public static void main(String[] args) {
        System.out.println("Taking Input From the User");
        Scanner sc = new Scanner(System.in);
//        System.out.println("Enter number 1");
//        int a = sc.nextInt();
//        float a = sc.nextFloat();
//        System.out.println("Enter number 2");
//        int b = sc.nextInt();
//        float b = sc.nextFloat();

//        int sum = a +b;
//        float sum = a +b;
//        System.out.println("The sum of these numbers is");
//        System.out.println(sum);
//        boolean b1 = sc.hasNextInt();
//        System.out.println(b1);
//        String str = sc.next();
        String str = sc.nextLine();
        System.out.println(str);

    }
}


Chapter 1 – Practice Set

  1. Write a program to sum three numbers in Java.
  2. Write a program to calculate CGPA using marks of three subjects (out of 100)
  3. Write a Java program that asks the user to enter his/her name and greets them with “Hello <name>, have a good day” text.
  4. Write a Java program to convert Kilometers to miles.
  5. Write a Java program to detect whether a number entered by the user is an integer or not.

Handwritten Notes: Click To Download

Ultimate Java Cheatsheet: Click To Download


Previous Post Next Post