User Input in JAVA

User Input in JAVA

Reading data from keyboard:

In order to read data from keyboard, JAVA has a Scanner class.
Scanner class has a lot of to read the data from the keyboard.
    Scanner s = new Scanner (System.in) [Read from the keyboard]
    int a = s.nextInt(); [Method to read data from the keyboard (Integer in this case)]

Code:

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

public class Tut4 {
public static void main(String[] args) {
System.out.println("Taking Input from User");
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number 1");
// int a = sc.nextInt();
// we can use float to calculate between decimal numbers
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 number is \n");
System.out.println(sum);
// boolean b1 = sc.hasNextInt();
// System.out.println(b1);
// String str = sc.next(); //do not read after space.
String str2 = sc.nextLine(); // whole line reading.
System.out.println(str);
}
}

Comments

Popular posts from this blog

JAVA Practice Set 2

Java Practice Set 4

Java Practice Set 3