#83 User Input using BufferedReader and Scanner in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

Udemy Courses:

In this lecture we are discussing about different ways to take input in java:
how to take input from user :
in C++ we use cin
in C we use scanf()
in python we use input()

How to take input in java?
#1
itconsidersr the firstcharacterr of enter sequence.

-- if we want to show result of multiple character we can use loop (not in video lecture forcuriosityy)
e.g
class Main{
public static void main(String[] args) throws Exception{
/*
input: a
output: 97
input: A
output: 65
input: 345 /considere 3 digit from number
output: 51
input: 3456 //consider 3 digit from number
output: 51
input: 3
output: 51

return ascii value of the input
*/
// to get actual number
// 1. convert ascii value to char

// 2. subtract 48 from the ascii value
//but it is only for single digit number

// formultiple-digittnumbersr we have to use loop
// 3. use loop
int n=0;
while(i!=13){ // 13 is ascii value of enter key
n=n*10+(i-48);
}

}
}
using InputStreamReader class:
In Java, the InputStreamReader class is used to read data from an input stream and convert it into characters.
It is often used with the BufferedReader class, which provides a buffered way to read characters from an input stream.
e.g
class Main{
public static void main(String[] args) {
BufferedReader br = null;
try {
// create a new BufferedReader to read from the InputStreamReader
br = new BufferedReader(isr);

// read a line of text from the BufferedReader

} catch (IOException e) {
}
finally{
if(br!=null){
try{
}
catch(IOException e){
}
}
}
}
}
Note: if open the resource then close is important

Use of Scanner Class :
To make programmer life easy
Scanner class was introduced in Java 1.5 as part of the Java API to provide an easy way
to read user input from various sources such as the keyboard.

a) Reading input through keyboard:

We then use the nextLine() method to read a line of text entered by the user.

Important: From here this part is not in video, for your cursoity we are put only in this description.

b) read through file

try {
Scanner scanner = new Scanner(file);
}
} catch (FileNotFoundException e) {
}
we create a Scanner object using a File object that represents the input file.
We then use the hasNextLine() and nextLine() methods to read each line of text from the file.

c) Read input though String

String input = "156 2 3 4 5";
Scanner scanner = new Scanner(input);
}
-- Scanner object using a String object that contains the input. We then use the hasNextInt() and nextInt() methods to read each integer from the string.

Рекомендации по теме
Комментарии
Автор

Really appreciate the way you taught this concept. Going through each of the class, methods, constructors and showing their implementations. I have never seen someone depicting the whole idea behind BufferedReader before. Thanks for this.

srimantamondal
Автор

thanks a lot
no useless talk
to the point, crisp short lecture
does the job

student_
Автор

Bro you are teaching the best way Humanity could have ever learned Java.

nrlvbur
Автор

you deserve more views diving really deep into some details that helps to better understand makes you really special from any other channel,
I think I found a treasure here

mowafkmha
Автор

And the beauty is..
I selected the best playlist to learn java😊.

yashaswinihm
Автор

Such clarity and beautiful explanation. You have always been my goto person when I need to understand something which I know I cannot understand from any other video. You are awesome.

tiwarikartikeya
Автор

Wonderful channel. You are one of the few people, which organize his courses in such a way, that i can quickly see in which series, a video is in.

nononnomonohjghdgdshrsrhsjgd
Автор

Great explanation and good examples. Thank you for this good and on point lecture.

dmsch
Автор

Great explainer -- tight -- all the best Sir

milehighgarage
Автор

Thanks for such deep dive in the class>object>class>method🦖
Hats Off to your way of explanation 🔥

_adarsh_raj_pathak_
Автор

Yes Elian ! I am waiting why you keep this video in #83 and i jumped from #13 video direct to #83 hahahahah

saqibafridi
Автор

Scanner was introduced in java 1.5
People before java 1.5 😅😅

sidhiqvs
Автор

can anyone explain why out is defined null and how it is working with null because when i write this program by classes and calling it in main, it gives an

Trading_Club
Автор

We are not getting the videos in order can you please rearrange it

alladiakhil
Автор

8:54 Telusko casually signaling he's part of the illuminati
Love the videos btw

shua_the_great
Автор

In the description,
File file = new file("input.txt")

Should'nt this line be given inside try?

sakthipriya
Автор

So out is a PrintStream type of reference initiated with null, then how it is used to calling println( ) which is a non static method right we need to create a object of PrintStream class so that we can access any non static members in that class

parthisanjay
Автор

if we don't close it still in java we have garbage collector

purohitvikramsingh
Автор

Ty for the video!!! Can scanner class be used with other resources besides command line input? for example a text file, or network input?

ferfykins
Автор

Better to use Scanner class right sir?

syedadil