#83 User Input using BufferedReader and Scanner in Java

  Рет қаралды 128,736

Telusko

Telusko

Күн бұрын

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
website : courses.telusk...
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
using System.in.read()
-- using System.in.read() we can take single character input only, if we provide multiplecharacterr
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{
int i =System.in.read(); // read a byte from the keyboard
System.out.println(i); // print the byte value
/*
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
System.out.println((char)i); // print the char value
// 2. subtract 48 from the ascii value
System.out.println(i-48); // print the actual number
//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);
i=System.in.read();
}
System.out.println(n);
}
}
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 InputStreamReader to read from System.in
InputStreamReader isr = new InputStreamReader(System.in);
// create a new BufferedReader to read from the InputStreamReader
br = new BufferedReader(isr);
System.out.println("Enter your name:");
// read a line of text from the BufferedReader
String name = br.readLine();
System.out.println("Hello, " + name + "!");
} catch (IOException e) {
System.err.println("Error reading input: " + e.getMessage());
}
finally{
if(br!=null){
try{
br.close();
}
catch(IOException e){
System.out.println("There might some problem to closing the resource");
}
}
}
}
}
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:
-- import java.util.Scanner; need to import in java file
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
-- Scanner object using the System.in input stream, which represents the 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
File file = new File("input.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
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
import java.util.Scanner;
String input = "156 2 3 4 5";
Scanner scanner = new Scanner(input);
while (scanner.hasNextInt()) {
int number = scanner.nextInt();
System.out.println(number);
}
-- 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.
Github repo : github.com/nav...

Пікірлер: 62
@srimantamondal8769
@srimantamondal8769 Жыл бұрын
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.
@student_03
@student_03 Жыл бұрын
thanks a lot no useless talk to the point, crisp short lecture does the job
@sanchitbajpaiexp7504
@sanchitbajpaiexp7504 3 ай бұрын
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.
@mowafkmha4505
@mowafkmha4505 Жыл бұрын
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
@yashaswinihm4288
@yashaswinihm4288 9 ай бұрын
And the beauty is.. I selected the best playlist to learn java😊.
@tiwarikartikeya
@tiwarikartikeya 5 ай бұрын
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.
@nononnomonohjghdgdshrsrhsjgd
@nononnomonohjghdgdshrsrhsjgd 10 ай бұрын
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
@nononnomonohjghdgdshrsrhsjgd 10 ай бұрын
What does "x:" and "a:" mean in Java in System.out.println(x:"Hello")?
@vishva8037
@vishva8037 2 ай бұрын
@@nononnomonohjghdgdshrsrhsjgd It is Vscode defaults there is no need to mentioned it
@md.ikramulislam9708
@md.ikramulislam9708 Ай бұрын
​@@nononnomonohjghdgdshrsrhsjgd suggestion by vs code due to an extenssion, you can ignore it.
@ParthMore-k4w
@ParthMore-k4w 2 ай бұрын
Bro you are teaching the best way Humanity could have ever learned Java.
@sy_37_siddhantmohekar62
@sy_37_siddhantmohekar62 22 күн бұрын
best playlist to learn java because most of other channels don't go in such depth
@kingkooper4627
@kingkooper4627 9 ай бұрын
best channel of the century 🎉🎉
@d0msch326
@d0msch326 3 ай бұрын
Great explanation and good examples. Thank you for this good and on point lecture.
@sakthipriya8653
@sakthipriya8653 Жыл бұрын
In the description, File file = new file("input.txt") Should'nt this line be given inside try?
@Trading_Club007
@Trading_Club007 11 ай бұрын
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 java.lang.NullPointerException
@jellyjollyjelly9513
@jellyjollyjelly9513 10 ай бұрын
precise and on point! love ur style
@parthisanjay3252
@parthisanjay3252 6 ай бұрын
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
@milehighgarage
@milehighgarage 4 ай бұрын
Great explainer -- tight -- all the best Sir
@_adarsh_raj_pathak_
@_adarsh_raj_pathak_ Жыл бұрын
Thanks for such deep dive in the class>object>class>method🦖 Hats Off to your way of explanation 🔥
@alladiakhil7425
@alladiakhil7425 Жыл бұрын
We are not getting the videos in order can you please rearrange it
@TheWildStatistician
@TheWildStatistician 9 ай бұрын
Going into insane detail! Well done!
@chandangouda6866
@chandangouda6866 10 ай бұрын
Which compiler are you using
@michaelp.channel331
@michaelp.channel331 3 ай бұрын
I love your explanation
@GokulSonawane-vi1gn
@GokulSonawane-vi1gn 2 ай бұрын
nice explanation sir.
@ferfykins
@ferfykins Жыл бұрын
Ty for the video!!! Can scanner class be used with other resources besides command line input? for example a text file, or network input?
@saqibafridi5292
@saqibafridi5292 Жыл бұрын
Yes Elian ! I am waiting why you keep this video in #83 and i jumped from #13 video direct to #83 hahahahah
@syedadil7256
@syedadil7256 Жыл бұрын
Better to use Scanner class right sir?
@ankushdhull7312
@ankushdhull7312 Жыл бұрын
yes....scanner class is far better than bufferedReader class!!
@vinayv6729
@vinayv6729 Жыл бұрын
@@ankushdhull7312 but scanner class is very slow
@syedadil7256
@syedadil7256 Жыл бұрын
@@vinayv6729 who cares bro... we need shortcuts
@vinayv6729
@vinayv6729 Жыл бұрын
@@syedadil7256 maybe but it does matter in cp.
@sonamohialdin3376
@sonamohialdin3376 Жыл бұрын
Very helpful tutorial
@darswayeeyou
@darswayeeyou Жыл бұрын
is out an object or object reference?
@Trading_Club007
@Trading_Club007 11 ай бұрын
it is an object BTW what is object reference
@md.ikramulislam9708
@md.ikramulislam9708 Ай бұрын
@@Trading_Club007 object variable or object reference variable points to the address of the actual object. //A obj=new A(); // here obj is not the object , it points to the object. new A() creates the object and obj points to it,without obj you cant access the object.
@varadavinay719
@varadavinay719 Жыл бұрын
10:12 😂😂😂 love your teaching man
@nikkg7055
@nikkg7055 Жыл бұрын
one more method PrintWriter..?
@Suresh_edits679
@Suresh_edits679 Жыл бұрын
nice sir
@mf3396
@mf3396 Жыл бұрын
Amazing class
@shua_the_great
@shua_the_great 5 ай бұрын
8:54 Telusko casually signaling he's part of the illuminati Love the videos btw
@sidhiqvs9227
@sidhiqvs9227 5 ай бұрын
Scanner was introduced in java 1.5 People before java 1.5 😅😅
@maleeshasandakalum6213
@maleeshasandakalum6213 Жыл бұрын
Thank you sir❤❤
@yeeshraj887
@yeeshraj887 Жыл бұрын
Can anyone tell which IDE is this?
@hemalathatummalapalli4636
@hemalathatummalapalli4636 Жыл бұрын
Intellij ide
@yeeshraj887
@yeeshraj887 Жыл бұрын
@@hemalathatummalapalli4636 Thank u
@nishant_singh
@nishant_singh Жыл бұрын
Its VS code not Intellij
@आयुष_उपाध्याय
@आयुष_उपाध्याय Жыл бұрын
​@@hemalathatummalapalli4636kya code krega re tu 😂😂
@anshsahu8290
@anshsahu8290 Жыл бұрын
Ascii or utf16 🤔
@purohitvikramsingh8229
@purohitvikramsingh8229 2 ай бұрын
if we don't close it still in java we have garbage collector
@raghavkaushik7266
@raghavkaushik7266 10 ай бұрын
In python just write input() done.
@yenaremadun7184
@yenaremadun7184 10 ай бұрын
but python is slow. There is always a trade off
@Progamer-fq8st
@Progamer-fq8st 7 ай бұрын
@@yenaremadun7184 in C++ you just write cin LOL
@nishant_singh
@nishant_singh Жыл бұрын
Buffer reader works faster than scanner...
@myvoice7558
@myvoice7558 Жыл бұрын
wow
@yt-1161
@yt-1161 Жыл бұрын
thumbs up
@charanmc4484
@charanmc4484 Жыл бұрын
rip Buffer reader 💀🐿️😅
@techfreak8854
@techfreak8854 Жыл бұрын
Do bufferreader still work?
@saurabh1087
@saurabh1087 Жыл бұрын
BufferedReader is faster than Scanner
@debajyotimitra369
@debajyotimitra369 Ай бұрын
Explanation AF
@naqibullahsultan4958
@naqibullahsultan4958 6 ай бұрын
just waste people time
#84 try with resources in Java
8:09
Telusko
Рет қаралды 52 М.
How Do We Get User Input in Java? - Full Tutorial
16:26
Coding with John
Рет қаралды 40 М.
She's very CREATIVE💡💦 #camping #survival #bushcraft #outdoors #lifehack
00:26
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 51 МЛН
Part 5. Roblox trend☠️
00:13
Kan Andrey
Рет қаралды 2,9 МЛН
Stream API in Java
26:04
Telusko
Рет қаралды 338 М.
Java File Input/Output - It's Way Easier Than You Think
8:18
Coding with John
Рет қаралды 466 М.
JWT авторизация. Основы JWT - механизма.
6:45
Хочу вАйти
Рет қаралды 11 М.
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 190 М.
the TRUTH about C++ (is it worth your time?)
3:17
Low Level
Рет қаралды 727 М.
#35 What is JWT and Why
14:47
Telusko
Рет қаралды 19 М.
INPUT-OUTPUT FUNCTIONS(SCANNER CLASS) - JAVA PROGRAMMING
29:11
Sundeep Saradhi Kanthety
Рет қаралды 163 М.
Java File I/O (Reading & Writing)
6:58
Keep On Coding
Рет қаралды 188 М.
How to accept user input in Java ⌨️【8 minutes】
8:02
Bro Code
Рет қаралды 204 М.
#5 What is Spring Boot?
12:12
Telusko
Рет қаралды 83 М.
She's very CREATIVE💡💦 #camping #survival #bushcraft #outdoors #lifehack
00:26