Java Inputs
Java Inputs
class GFG
{
// Main Method
public static void main(String[] args) throws IOException
{
// Creating BufferedReader Object
// InputStreamReader converts bytes to
// stream of character
BufferedReader bfn = new BufferedReader(new InputStreamReader(System.in));
// Printing String
System.out.println("Entered String : " + str);
// Printing Integer
System.out.println("Entered Integer : " + it);
}
}
Output:
Mayank Solanki
888
Entered String : Mayank Solanki
Entered Integer : 888
class Easy
{
public static void main(String[] args)
{
// creating the instance of class BufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name;
try
{
System.out.println("Enter your name");
name = reader.readLine(); // taking string input
System.out.println("Name=" + name);
}
catch (Exception e)
{
}
}
}
Output:
Enter your name:
Geeks
Name=Geeks
Method Description
int nextInt() It is used to scan the next token of the input as an integer.
float nextFloat() It is used to scan the next token of the input as a float.
double nextDouble() It is used to scan the next token of the input as a double.
byte nextByte() It is used to scan the next token of the input as a byte.
String nextLine() Advances this scanner past the current line.
It is used to scan the next token of the input into a boolean
boolean nextBoolean()
value.
long nextLong() It is 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() It is used to scan the next token of the input as a BigInteger.
BigDecimal It is used to scan the next token of the input as a
nextBigDecimal() BigDecimal.
Hence, in the case of Integer and String in Scanner, we don’t require parsing as we did require
in BufferedReader.
// Java Program to show how to take
// input from user using Scanner Class
import java.util.*;
class GFG
{
public static void main(String[] args)
{
// Scanner definition
Scanner scn = new Scanner(System.in);
// print String
System.out.println("Entered String str1 : " + str1);
// print string
System.out.println("Entered String str2 : " + str2);
// input is an Integer
// read by nextInt() function
int x = scn.nextInt();
// print integer
System.out.println("Entered Integer : " + x);
// input is a floatingValue
// read by nextFloat() function
float f = scn.nextFloat();
Example-2:
// Java Program to implement
// Scanner Class to take input
import java.io.*;
import java.util.Scanner;
// Driver Class
class Easy
{
// main function
public static void main(String[] args)
{
// creating the instance of class Scanner
Scanner obj = new Scanner(System.in);
String name;
int rollno;
float marks;