Code For BufferedReader Class Example in Java

Code For BufferedReader Class Example in Java
Apple iPhone 3G – 8GB – Black (Factory Unlocked) Smartphone- Fully Functional!
Current Bid: $80.00

How to use BufferedReader?

One of the uses of BufferedReader in Java is to get the value of the variable input by the user, very much like what scanner does. The class BufferedReader should be also associated with an object. This object would be used to access the class BufferedReader. This is how we declare the bufferedReader with its own object:

BufferedReader putAnObjectHere = new BufferedReader(new InputStreamReader(System.in));

After that, you can import the packages associated with the declaration above. There are actually 2 packages that you need to import. These packages are:

import java.io.BufferedReader;

import java.io.InputStreamReader;

If you use netbeans, simply click the red error sign then click the first choice, do that twice. This is to automatically import the 2 packages above your main program and below the program’s package name.

Using the BufferedReader object on getting the integer input

To get the integer input by the user, simply declare a variable then access the BufferedReader class. The variable should be also declared in a String data type then parse it to integer in order to work. When I tried directly to declare an int, then print it on the screen, it doesn’t work correctly. See the example below on how to do it.

Apple iPod nano 7th Generation Blue (16 GB) (Latest Model)
Current Bid: $113.49
Apple iPod touch 5th Generation Black & Slate (32 GB) (Latest Model)
Current Bid: $248.89

String inputBytheUser = input.readLine();

int parseInputBytheUser = Integer.parseInt(inputBytheUser);

Unlike the class Scanner, BufferedReader always need other packages to be imported before you can use it. In the first declaration above, you still need to use the IOException package in order for the code to work. This is how to import the exception:

import java.io.IOException;

If it is a little cloudy, you can refer to the code below for a better example.

NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96

Using the BufferedReader object on getting the String input

The highly use data types for beginners are always integer and string, so in this hub, we will also show you on how get the input of the user on the string data type. This is how we will do it:

String StringInput = input.readLine();

If you already import a package for IOException, no need to import for this, it will work

Sample Java Program for BufferedReader

package bufferedreader_in_java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Input a String: “);
String StringInput = input.readLine();
System.out.println();
System.out.print(“Input an Integer: “);
String integerInput = input.readLine();
int ParseIntegerInput = Integer.parseInt(integerInput);
System.out.println();
System.out.println(“The Integer Input: ” + ParseIntegerInput);
System.out.println(“The String Input: ” + StringInput);
}
}

Java Books

Head First Java, 2nd Edition, Kathy Sierra, Bert Bates, Sierra Kathy, Bates Be
Current Bid: $19.14
Data Structures and Algorithm Analysis in Java by Mark A. Weiss 3rd ,3e 3ed
Current Bid: $46.48

Related Java Tutorials

5 Important Tips to Learn Java Programming and Other Programming Languages
Basic Knowledge Required in Programming
How to Program in Java Using Netbeans: Complete Simple Easy Steps
Java Simple Codes for Beginners
Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
Java Tutorial Examples

Java Video Tutorial

Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99

Other Java Source Codes

Java Program: How to Use If Statement in Java Programming
Java Program: How to Use If-Else Statement in Java Programming
Java Program: Using Multi If and Else Statement in Java Programming
Java Source code sample: How to Add Numbers inside an Array using For Loop
Java Program: How to Use Switch Statement in Java
Java Program: How to Parse a String into Integer in Java Programming
Java Program: Count the Number of String Characters in Java
Java Source code: How to Add numbers inside an Array Using Recursion
Java Source Code: A Recursive Asterisk Diamond Shape Program
Java Program: Reverse String in Java Using For Loop
Java Program: Palindrome Test Java Source Code
Java Program: How to Sort Numbers in an Array in a Descending Order
Java Program: How to Sort Numbers in Ascending Order
Sample Java Program for String Tokenizer
Sample Program for JOptionpane and Java Source Code for Basic Calculator
What is a Java Scanner Class and How to Use it?
What is MDAS? Sample Java Source Code for MDAS
What is a Static Method in Java?
Casting in Java