How to get input from user in Java

Programming languages or concepts
0

  in this tutorial we will learn how java get input from user in java. java brings various streams with its I/O package which helps the user to perform all input-output operations. These streams support all types of objects, data types, characters, files, etc. to perform I/O operations. It can take input in four ways, either from the user or from a file


BufferReader class

Scanner class

Using Console Class

Using File Handling


Java Scanner Class

The Java Scanner class allows the user to receive input from the console.
This is an advanced version of BufferedReader that was added in later versions of Java. A scanner can read formatted input. It has different functions for different types of data.
 It belongs to java.util package.
A scanner is very easy to read because we don't need to write a throw because there is no exception thrown in it.
It was added in later versions of Java
It has predefined functions for reading integers, characters and other data types.

 It is used to read input of primitive types like int, double, long, short, float and byte. This is the simplest way to read input from a Java program.

Syntax for importing Scanner Class:

import java.util.Scanner;  

Syntax

Scanner sc=new Scanner(System.in);  

The above syntax creates a constructor of the Scanner class with System.in as an argument. 
This means it is going to be read from the program's standard input stream. 
The java.util package must be imported when using the Scanner class.

import java.util.*; 

class AdditonDemo 

{ 

public static void main(String[] args) 

{ 

Scanner sc= new Scanner(System.in);  

System.out.print("Enter first number "); 

int a = sc.nextInt(); 

System.out.print("Enter second number "); 

int b= sc.nextInt(); 

 

int c = a + b ; 

System.out.println(" Addition of two numbers is = " +c); 

} 

} 

Output :

java -cp /tmp/L4TkW7X9nO AdditonDemo

Enter first number 23

Enter second number 23

Addition of two numbers is = 46


Methods of Java Scanner Class

Java Scanner class provides the following methods to read different primitives types:

Method

Description

int nextInt()

This method used to scan the next token of the input as an integer.

float nextFloat()

This method used to scan the next token of the input as a float.

double nextDouble()

This method used to scan the next token of the input as a double.

byte nextByte()

This method used to scan the next token of the input as a byte.

String nextLine()

Advances this scanner past the current line.

boolean nextBoolean()

It is used to scan the next token of the input into a boolean value.

long nextLong()

This method used to scan the next token of the input as a long.

short nextShort()

It is used to scan the next token of the input as a Short.

BigInteger nextBigInteger()

This method used to scan the next token of the input as a BigInteger.

BigDecimal nextBigDecimal()

This method used to scan the next token of the input as a BigDecimal.

 

BufferedReader

This is a simple class that is used to read a sequence of characters. It consists of a simple function that reads one character after another, an array of characters, and a readLine() function that reads a line.InputStreamReader() is a function that converts an input stream of bytes into a stream of characters so that it can be read by BufferedReader as it expects a stream of characters.

Post a Comment

0Comments

Post a Comment (0)
close