Java Programs to Read numbers from Standard Input

Introduction

There are several ways by which you can read numbers into a Java program. Some of the various ways to read numbers from standard input are the Scanner class. Other than the Scanner class, the BufferedReader class is used quire often to input numbers. This class provides several methods related to the input of different primitive types. In this article, we will look at several java programs that will allow us to read numbers from standard inputs.

The various ways to read a number are as follows:

  • Using Scanner Class
  • Using BufferedReader Class
  • Using Command-Line Arguments

Using Scanner Class

If you want to use the Scanner Class in your code, you will have to import the java.util.* package. This package requirement is a hard mandate is you are working with Scanners. The Scanner Class and its various methods can be used to take input of different primitive types, such as int, double, long, char, etc.

Firstly, an object of the Scanner class has to be made and then the next() method can be invoked to read data from the user.

Scanner scan = new Scanner ();

Some of the other methods that are available are nextDouble(), nextLong(), and nextFloat(), etc.

Sample Program

import java.util.Scanner;
public class Demo1
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println (“Enter a number: ”);
int num = sc.nextInt();
sc.close();
System.out.println (“The number entered by the user is ” + num);
}
}

OUTPUT

Enter a number: 89
The number entered by the user is: 89

Using BufferedReader Class

The BufferedReader class is mainly used to read the text from a character-based input stream. You can read an entire line of the input given by the user using the readLine() method. This enhances the performance and the BufferedReader class inherits the Reader class in Java. This class, along with other relevant classes, is included in the java.io package, therefore this package has to be imported before you can use BufferedReader in your program. In order to take an input, the first step is to create a constructor of the BufferedReader class and parse a Reader as a parameter. An object of the InputStreamReader class is generally parsed. Therefore, the parseInt() method of the Integer class has to be invoked which will inturn parse the readline () method of the BufferedReader class. As mentioned before, the readLine() method reads one line of text.

Sample Program

import java.io.*;
public class Demo
{
public static void main (String args []) throws IOException
{
InputStreamReader read = new InputStreamReader (System.in);
BufferedReader in = new BufferedReader (read);
System.out.println (“Enter a number”);
int number = Integer.parseInt(in.readLine());
System.out.println (“You have entered” + number);
}
}

OUTPUT

Enter a number: 23
You have entered: 23

Instead of breaking down the BufferedReader source code in the above program, it can be compactly written as follows:
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader (read);

OR

BufferedReader in = new BufferedReader (new InputStreamReader (read));

Using Command-Line Arguments

When you are using Command Line Arguments, the input is normally read at the time of execution of the program. The arguments that a user passes through the console can be passed through to the program as input. When the command line arguments are received by the JVM, the numbers are wrapped and transferred to args [].

In the following program, two numbers have been taken as input from the command line and then converted into integers using the Integer.parseInt() method because only strings are accepted in command line arguments.

User Guide to Run the Program

  • Save the program with the name java
  • Compile the program by typing the command javac Demo.java
  • Run the program by typing the command java Demo 12 90

Here 12 and 90 are the command line inputs to the program:

public class Demo
{
public static void main (String args [])
{
int a,b,sum;
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
sum = a + b;
System.out.println (“The sum of” + a + “and” + b + “is” + sum);
}
}

OUTPUT

The sum of 12 and 90 is 102.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.