Reading Input from Keyboard-using Scanner Class


Reading input from user

A program without any interactin is of no use. We must have some way of talking and asking the program to provide some sort of inputs.

Scanner class is used to get the input from a keyboard, from a file etc. Here is an example:

What you need to do:

a. declare variable to hold values from input(keyboard)

b. declare Scanner class object using Scanner kb = new Scanner(System.in);Here kb is an object of Scanner class that makes connection with the keyboard by the scanner class and in the new Scanner() pass System.in method to read from the input device

c. Repeat the above step-b for more variables

d. declare a variable double to hold the average of the two numbers.

Here is an example:

package com.Examples;
import java.util.Scanner;
public class readingInputs
{
public static void main(String[] args)
{

int firstnmmber;
int secondNumber;
Scanner kb = new Scanner(System.in);
System.out.println(“Please enter afirstNumer”);
firstnmmber =kb.nextInt();
System.out.println(“Please enter second number”);
secondNumber=kb.nextInt();
double average;
average=(firstnmmber + secondNumber)/2.0;
System.out.println(“Average of two numbers =”+average);
}

}

output:

Please enter afirstNumer

30

Please enter second number

25

Average of two numbers =27.5

What are different types of values can be entered through Scanner class:

The Scanner class has number of following methods to read inputs:

Sl
Scanner object.next…() method
Value returned
1
nextInt()
Returns Integer
2
nextByte()
Returns Byte
3
nextBoolean()
Returns boolean type
4
nextLine()
Returns string
5
nextDouble()
Returns double
6
nextFloat()
Returns float

Example:

package com.Examples;
import java.util.Scanner;
public class readingInputs
{

public static void main(String[] args)

{
int firstNumber;
int secondNumber;
Scanner kb = new Scanner(System.in);
System.out.println(“Please enter firstNumber “);
firstNumber=kb.nextInt();
System.out.println(“Please enter secondNumber”);
secondNumber=kb.nextInt();
double average;
average=(firstNumber + secondNumber)/2.0;
System.out.println(“Average of two numbers is :”+ average);
String name;
String surname;
System.out.println(“Please enter your first name”);
name=kb.nextLine();
System.out.println(“Please enter your surname”);
surname=kb.nextLine();
System.out.println(“name and Surname is “+ name + ” ” + surname);
}

}

Output:

Please enter firstNumber

30

Please enter secondNumber

55

Average of two numbers is :42.5

Please enter your first name

Please enter your surname

Rajotiya

Observe the problem with the string input. The program doesn’t prompt for inputting the name, but instead skips this line and goes to next line to get value of the surname.

What is this problem?

If you have used any of the primitive type methods such as nextInt(), nextBoolean, nextByte(), nextDouble()…etc………… and lateron after these primitive type methods you try to call nextLine() method, you will have this problem.

When a user calls a newInt() methods and enter something, it creates a newline character in the buffer of the cmputer

What is the solution?

So solution is to consume that newline character created in the computer buffer. This is done by calling:

kb.nextLine(); this will consume that new line character. This problem would not have arisen if we were using only the nextLine() method and not the nextInt(), nextDouble(), nextShort(), nextByte(),nextBoolean() etc before that.

Now modify the abobe program with a blank kb.nextLine(); and see the system prompting fr all inputs.

package com.Examples;

import java.util.Scanner;

public class readingInputs
{
public static void main(String[] args)

{

int firstNumber;
int secondNumber;
Scanner kb = new Scanner(System.in);
System.out.println(“Please enter firstNumber “);
firstNumber=kb.nextInt();
System.out.println(“Please enter secondNumber”);
secondNumber=kb.nextInt();
double average;
average=(firstNumber + secondNumber)/2.0;
System.out.println(“Average of two numbers is :”+ average);
String name;
String surname;
System.out.println(“Please enter your first name”);
kb.nextLine();
name=kb.nextLine();
System.out.println(“Please enter your surname”);
surname=kb.nextLine();
System.out.println(“name and Surname is “+ name + ” ” + surname);
}

}
Output:

Please enter firstNumber

30

Please enter secondNumber

45

Average of two numbers is :37.5

Please enter your first name

Ravinder

Please enter your surname

Rajotiya

name and Surname is Ravinder Rajotiya

Process finished with exit code 0

The output and the prompt for all inputs are perfectly OK

 

 

 

 

 

error: Content is protected !!