java console input tutorial

The Scanner JAVA class is responsible of reading input from the console.

There are other ways to read data from the console, but the Scanner class is the way to do it.

We will present how to read each data type from the console using the JAVA Scanner class.

We will also present another method to read data from the console using the classes BufferedReader and InputStreamReader.

The JAVA Scanner class belongs to the java.util package. So, we must import it in order to use it in our program.

JAVA Console Input: Read an int From Console

import
java.util.Scanner;
 
public class TestReadFromConsole {
 
public static void main(String[] args) {
int nbr;
Scanner sc = new Scanner(System.in);
System.out.println("Give me an iteger : ");
nbr=sc.nextInt();
System.out.println("The Given integer is equal to "+nbr);
} 
}

JAVA Console Input: Read a float From Console

The floats are written in different formats in Europe and the USA.

So, we should mention the locale to avoid the InputMismatchException exception.

If we append an F to the float literal we will get the InputMismatchException.

This exception means that the String that represents the float cannot be converted to a float.

import java.util.Locale;
import java.util.Scanner;
 
public class TestSand {
 
public static void main(String[] args) {
 
float nbr;
Scanner sc = new Scanner(System.in);
//use US locale to identify floats written in the US Format
sc.useLocale(Locale.US);
System.out.println("Give me a float : ");
nbr=sc.nextFloat();
System.out.println("The Given float is equal to "+nbr);
}
}

JAVA Console Input: Read a double From Console

What we said about reading a float from the console (the locale) is valid for the double.

import java.util.Locale;
import java.util.Scanner;
 
public class TestSand {
 
public static void main(String[] args) {
double nbr;
Scanner sc = new Scanner(System.in);
//use US locale to identify floats written in the US Format
sc.useLocale(Locale.US);
System.out.println("Give me a double : ");
nbr=sc.nextDouble();
System.out.println("The Given double is equal to "+nbr);
}
}

JAVA Console Input: Read a String from Console

import
java.util.Scanner;
 
public class TestSand {
 
public static void main(String[] args) {
String s;
Scanner sc = new Scanner(System.in);
System.out.println("Give me a string : ");
s=sc.nextLine();
System.out.println("The Given double is equal to "+s);
} 
}

JAVA Console Input: Other method to read input from console

This is another way to read input from the console using the classes BufferedReader and InputStreamReader.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class TestSand {
public static void main(String[] args) {
System.out.println("Give me a string : ");
try{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println("The Given String is : "+s);
}
catch(IOException e)
{
e.printStackTrace();
}
} 
}

This core JAVA tutorial which deals with java console input arrives at its end.

To check the other Core JAVA tutorials statements, please check this tutorial.

Please, don’t forget to follow us on Facebook to get our next JAVA core tutorial.

Post a Comment

Previous Post Next Post