need help. while I'm inputing inegers all handling work fine, but when I entered string variable programm start internal loop, any ideas? code below
Scanner inp = new Scanner(System.in); int x=1; do{ try{ System.out.println("input first num"); int n1= inp.nextInt(); System.out.println("input second num"); int n2 = inp.nextInt(); int result = n1/n2; System.out.println(result); x=2; }catch(Exception e){ System.out.println("this is wrong"); } }while (x==1);
For you guys that are getting infinite loops when entering an invalid character, such as a letter, you will need to call next() on your scanner inside the catch clause to clear the invalid data from the scanner, else, you will be looping over invalid data.
i tried to input 0.1 for d 2nd number,,,, and i think it failed on catching the error,,, but of course it could b resolved by applying what u hav learned from bucky's lessons combined wid ur analytical skills....
Actually, errors and exceptions are not the same thing according to Java's website. Error is supposed to be more serious, a fundamental break down in the Java virtual machine side of things while running the program whereas an exception takes place due to bad programming.
oh, now I've got how to make this run properly: create Scanner object INSIDE try code. I don't know why in other case it looks like last entered value is still assigned to input.nextInt()
Epic glitch, when you insert letters (YES, ITS POSSIBLE) and press enter it keeps saying enter first num: You cant do that. really fast. lol it wont stop :(
o thought it needed a counter to keep check of how many times x has been used thats why it ran more that 2 times where in the try block i think x=2 should be x++ just saying
i can't get multiple try&error-blocks to work properly.
if i create one with lets say try{ int a = 5/0; } catch(Exception e){ /* do something */ }
and then add another one which does pretty much the same thing, the second catch-block is called before java even tries do call the 2nd try-block (yes, the second Exception-Object is called different).
can someone pllzzzz tell me how can i handle the context of a file? for example lets say taht i want to find someone whith his last name and print his age . :( plzzz
@Vlaxiti You do the exact same thing as what Bucky did and then in the while(x.hasNext()) loop you place a if statement which monitors if your a,b or c is what you're looking for, and if it is,then you make it print what you want it to.
@tyebillion Boolean in reality is not different than an integer. Boolean is just an integer that takes 1 or 0 as values. You would notice that(and a lot more) if you tryied learning C.
@Muse94Muse that is cause the code needs a int number, 20.5 is a float number, since you din't put a int it gonna be asking for the first int. i'm just a begginer so please forgive me if i'm wrong.
hey gud job bucky hope in India we get rid of all the stupid prof and we culd really use ur tutorials all the students will get clear off their subjects......lol....
hey gud job bucky hope in India we get rid of all the stupid prof and we culd really use ur tutorials all the students will get clear off their subjects......lol....
This was a great video because. The speaker is up beat and funny. Also he reiterates which drives home the point. Also he tries several examples to show what he is saying. So now I want to watch the other videos!
thank you very much. i have a question, is there a way to do system clear screen on java? i saw people saying mod the file or download a new java package etc...but is there a way to do system clear screen without moding the file or download java pack? or do u have a video of it? BTW UR AWSOME
Exceptions and errors are NOT the exact same thing. Firstly, because errors are unchecked, and exceptions (beside RuntimeException) are checked. Then, errors are severe problems, like OutOfMemoryError.
For those who read my comment from yesterday by only adding input.next(); it only checks for the first token. And then each token after seperately. So if you add what I show above and then type in "one two three four" it will say You cant do that and then Enter first num: four times before letting you enter a new value. By adding input.nextLine(); it takes everything entered as one and lets you enter a new value.
For those of you wondering how to stop it from going into an infinit loop once you enter anything other than an integer. Insert input.next(); underneath System.out.println("You cant do that"); This will reset the value of input so that it will ask for you to input the first num again.
can somebody tell me how to stop this code from looping to infinity when an exception is caught? void Class(){ int y=1; do{ try{ double array[]=new double[5]; for(int x=1;x<array.length;x++){ System.out.println("enter value for index"+y); array[x]=input.nextDouble(); } y=2; }catch(Exception e){ System.out.println("wrong!"); } }while(y=1); }
I know each try MUST have a catch, but can a try have more than one catch? For example, if i wanted to have one message for one error, and another message for the another specific error?
Thanks boss! This gives me a great start on solving my problem in my program. trying to throw a exception made by me to prompt the user to only input a, b, or y. But it says the code doesn't throw an exception.
So we had ifception, now it's catch 22
TheViolinCalamity 1 week ago in playlist Java (Beginner) Programming Tutorials
to Spielberg2Cents, why have you spelt, dividing as "diving", just saying...
hashtit 2 weeks ago
@hashtit Cuz, in buckys tutorials, you go ahead and make sure you haue speleed everryting rong
iCrimZen 1 week ago
only 66,000 people really liked java and tried to learn it... im one of em
l1998 2 weeks ago
I accidentally typed in a letter...the Loop is never stopping. Explain please...
etritb 4 weeks ago
Yaayyy atlast..a new and meaningful main class name
usama8800 1 month ago in playlist Java Programming Tutorials
helpful
Boss201152 1 month ago
This is the first video in a while that I've had to watch only once to completely understand it :DDDD
lNUMPUN 2 months ago
I entered a letter and I got an infinite loop of
You can't do that.
Enter first number:
You can't do that.
Enter first number:
Any suggestions?
The rest works fine, if I try to divide by zero it catches the error normally.
omg123596 2 months ago
@omg123596 what is your code?
molle7397 1 month ago
@molle7397 I think I might of fixed it, I'll send you another reply if it doesn't work out.
omg123596 1 month ago
@molle7397 It wont let me post the code, can I PM you my email?
omg123596 1 month ago
@JSA19882007 thanks :)
muazambce 2 months ago
I bet 80% of the 1.1 million people who stopped watching were here solely for minecraft.
MineCrafterBrad 2 months ago
what is the name of this compiler ??
can anyone telme about this ??
muazambce 2 months ago
@muazambce Eclipse
JSA19882007 2 months ago
if you enter a letter for a number it spazzes out, how can you fix that?
KrazyMexicanm 2 months ago
try{ ... }catch(ArithmeticException e){ System.out.println("You can't divide by zero!") }catch(InputMismatchException e){ System.out.println("Wrong input type. Number expected.") }
I think that works.
gatoo23 2 months ago
}catch(Exception e){ System.out.println("u r idiot))"); inp.next();
}
helps to escape infinite loop if wrong type of data entered
ShadowOfAlbion 2 months ago
need help. while I'm inputing inegers all handling work fine, but when I entered string variable programm start internal loop, any ideas? code below
Scanner inp = new Scanner(System.in); int x=1; do{ try{ System.out.println("input first num"); int n1= inp.nextInt(); System.out.println("input second num"); int n2 = inp.nextInt(); int result = n1/n2; System.out.println(result); x=2; }catch(Exception e){ System.out.println("this is wrong"); } }while (x==1);
ShadowOfAlbion 2 months ago in playlist Bucky's Java Tutorial
This is good. I hope someday I could make a java tutorial.
PirkkoPihtipudas 2 months ago
An error occurred when you were talking about catching errors.
THANKS!
ubhisingh93 2 months ago
For you guys that are getting infinite loops when entering an invalid character, such as a letter, you will need to call next() on your scanner inside the catch clause to clear the invalid data from the scanner, else, you will be looping over invalid data.
tatersalad58 3 months ago 11
@tatersalad58 you dont, its gonna just read another num
just at the start of the loop we need to clear wrong input data using
input.reset();
devilazpl 4 weeks ago
no exception of type Exception can be thrown; an exception type must be of a subclass of Throwable
JKTCGMV13 3 months ago
1:06 I think you mean a scanner variable called input LOL.
gatoo23 3 months ago
Dont type 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 for your first num -.-
yourock129 4 months ago in playlist Java Programming Tutorials
@yourock129 i made an exception for that my calculator :)
zekage2 4 months ago
I have you to thank for my degree! :)
auradistorter 5 months ago
THANK YOU!!!!
krzysiriem 5 months ago
now try entering a letter XD
DazKins 5 months ago
i tried to input 0.1 for d 2nd number,,,, and i think it failed on catching the error,,, but of course it could b resolved by applying what u hav learned from bucky's lessons combined wid ur analytical skills....
kenshinvrix 6 months ago
Actually, errors and exceptions are not the same thing according to Java's website. Error is supposed to be more serious, a fundamental break down in the Java virtual machine side of things while running the program whereas an exception takes place due to bad programming.
DemocraWatch 6 months ago in playlist Bucky's Java
@DemocraWatch import Futurama.References;
Shaddap square!
DarkHentaiDragon 4 months ago
Why is your variable named sum when you're diving??? ;-) .. just saying.. :-D
Spielberg2Cents 6 months ago 27
Okay, why is my program so awesome? It divides by zero!!!!! It gives the answer of INFINITY!!!! Who else has a smart program? :D
Arc72Productions 6 months ago
@Arc72Productions oh! my program is like that cause I used doubles, since division can come up with rational numbers, not just whole ones.
Arc72Productions 6 months ago
This is fun! :):):):):
MrMacieyas 6 months ago
oh, now I've got how to make this run properly: create Scanner object INSIDE try code. I don't know why in other case it looks like last entered value is still assigned to input.nextInt()
rychu130 6 months ago
why don't you start try block after print and scan statements ?
saa442 6 months ago
Booleans exist....
Kevineatsbabies 7 months ago
you saved my life
s2003m0205 7 months ago
e.printStackTrace();
seed419 7 months ago in playlist Bucky's Java Tutorial
Epic glitch, when you insert letters (YES, ITS POSSIBLE) and press enter it keeps saying enter first num: You cant do that. really fast. lol it wont stop :(
FreePKMNWalkthroughs 7 months ago 4
@FreePKMNWalkthroughs
It does it if you put there any letter or basically another type of data ( like 3,5)
rychu130 6 months ago
the way you pronounce "num" sounds like saying "dick" in Tunisian.
hexmedi 7 months ago
A neat trick to use instead of the do-while loop is a infinite for loop:
for(;;)
{
}
dabu002 8 months ago
if u try to divide letters... u can't close the console..T___T
PS3GAMEPWNERZ 8 months ago
you can also say while(true){blabla,
break; //cancels a loop
}
Great tutorials. I'm closing in on your game tutorials! =)
Zorobay 8 months ago
BAM! Epic help _0_
adri144n 8 months ago
BAM! we finally made it all the way, just awesome bucky
emblaz3 8 months ago
it's "pretty much" explained and got it finally : ) thanx
Drk86 8 months ago
lol "I cant let you do that Dave"
eeg10 9 months ago
some idiot did divide this by zero
deadmanwalks09 9 months ago in playlist Java Programming Tutorials
o thought it needed a counter to keep check of how many times x has been used thats why it ran more that 2 times where in the try block i think x=2 should be x++ just saying
Cyberdyne22 9 months ago
i tried to devide by + and it crashed, it sad You cant do that! over and over.
lol XD
nargraestior 9 months ago
thank you so much for that x=1 and x=2 things. It really help out my project :) Keep doing your best!
hale1278 9 months ago
i can't get multiple try&error-blocks to work properly.
if i create one with lets say try{ int a = 5/0; } catch(Exception e){ /* do something */ }
and then add another one which does pretty much the same thing, the second catch-block is called before java even tries do call the 2nd try-block (yes, the second Exception-Object is called different).
what am i doing wrong?
Booone008 10 months ago
it's funny cause in the earlier tutorials he started with num1 variable now he's gotten more lazy down to n1
Khoaster 10 months ago
Gave me infinity :D
jakeywakey27 10 months ago
can someone pllzzzz tell me how can i handle the context of a file? for example lets say taht i want to find someone whith his last name and print his age . :( plzzz
Vlaxiti 10 months ago
@Vlaxiti You do the exact same thing as what Bucky did and then in the while(x.hasNext()) loop you place a if statement which monitors if your a,b or c is what you're looking for, and if it is,then you make it print what you want it to.
marsimil 10 months ago
@marsimil thank you very much ::)
Vlaxiti 10 months ago
In my version of this program I used a boolean variable for x.
tyebillion 10 months ago
@tyebillion Boolean in reality is not different than an integer. Boolean is just an integer that takes 1 or 0 as values. You would notice that(and a lot more) if you tryied learning C.
pithikoulis 10 months ago
Thanks again man
nyarachm 10 months ago
hey bucky you did not continue on exception handling. :( thanks for your help so far! :)
lyon87 10 months ago
I love the way Bucky simplifies the concepts instead of throwing in a bunch of other concepts at the same time, the way Deitel and Deitel does.
martywme 11 months ago
man you are the best
baalkta 11 months ago
You really gotta watch out for those dumb users nowadays.
moufpuncha 11 months ago
@thenewboston I tried what you said but it seems to retain the first number that the user put in and thus get stuck in a circle of saying
You can't do that
Enter first num:
You can't do that
Enter first num:
etc etc. any tips?
jmmycnnll 11 months ago
@jmmycnnll Are you using ints or doubles?
Vicininater 11 months ago
thanx man...u did a gud job
240470304551029 1 year ago
LOL ... cause i'm lazy. That's funny.
patwasikus 1 year ago
if i divide it 20.5 or something it just keeping writing Enter first num: over and over again
Muse94Muse 1 year ago
@Muse94Muse that is cause the code needs a int number, 20.5 is a float number, since you din't put a int it gonna be asking for the first int. i'm just a begginer so please forgive me if i'm wrong.
igrossiter2 1 year ago
I'm pretty sure he could write while(true) in the place of do, and in catch he could write continue and in try he could write break.
MCPiciek 1 year ago
Errors and exceptions are not the same thing.
An error causes the system to stop working, so you cannot recover.
An exception means that a problem occur, but you can recover from it.
cperez1000 1 year ago
don't put a letter it just says enter first number: you can't do that for ever
gowlp 1 year ago
This has been flagged as spam show
If you really want to marry an Latino women come to us rockmycity.info
honsusando 1 year ago
If you're dividing by zero, you have no business in the stock market..
Just saying x.x
DizzyThermal 1 year ago 26
why does it cause an infinite loop if you enter a character?
gmorphis 1 year ago
@gmorphis if you add a
input.nextLine();
in your catch it disregards the input so the user can try again
gmorphis 1 year ago
@gmorphis Thanks for that [nine months ago] any idea why the program crashes if you use input.nextInt instead of input.nextLine?
DarkHentaiDragon 4 months ago
So pretty much we use loops whenever we want the program to not move on to the next line of code if it encounters an error?
4thKyuubi 1 year ago
Hey bucky, thanks for the videos, they help a lot.
Does anyone know what IllegalArgumentException handles?
4thKyuubi 1 year ago
there's always a catch
gf133 1 year ago
please make a tutorial concerning throw exception plus looping and binary search? I'm sure you can do that.. LOL.. thnx
dotahyper 1 year ago
Hi, bucky, you're awesome!
Can you make a tutorial about throw exception??
WmAlvis 1 year ago
Thanks boss. It's very useful for me
dineshaccount 1 year ago
Thanks boss. It's very useful for me
dineshaccount 1 year ago
THANK YOU !
aravok 1 year ago
hey gud job bucky hope in India we get rid of all the stupid prof and we culd really use ur tutorials all the students will get clear off their subjects......lol....
mayureshh101d 1 year ago
hey gud job bucky hope in India we get rid of all the stupid prof and we culd really use ur tutorials all the students will get clear off their subjects......lol....
mayureshh101d 1 year ago
what if we built a huge program for analyzing the stock market.. and it was awesome...
XD I LOLD!
scottycali4nia 1 year ago
YOU ARE GOD
LordCarringtan 1 year ago
almost done with the whole series!!!!!!!!! YOU ROCK SIR... YOU ROCK!!
jafarkay 1 year ago 2
dude you rock! great explanation... i'm doing some reviewing from my Senior lvl courses in Java =_=
yayo51 1 year ago
Thanx! I have exam today, watching you tutorials for revision. So clear and understandable! =)
you are awesome :-)
LiudaMikha 1 year ago
This has been flagged as spam show
This was a great video because. The speaker is up beat and funny. Also he reiterates which drives home the point. Also he tries several examples to show what he is saying. So now I want to watch the other videos!
MadMasterInventor 1 year ago
Comment removed
MadMasterInventor 1 year ago
thank you very much. i have a question, is there a way to do system clear screen on java? i saw people saying mod the file or download a new java package etc...but is there a way to do system clear screen without moding the file or download java pack? or do u have a video of it? BTW UR AWSOME
mewanafly 1 year ago
Thank you so much.....
saad0011 1 year ago
really easy , thank u so much
lovehowlwolf 1 year ago
veryyyyyyyyyyyyyyyyyy nice exampleeeeeeeeee
harjit303 1 year ago
thanks alot sir your tutorials are great
znmansoor 1 year ago
Comment removed
znmansoor 1 year ago
1:01 You said: "Now we have an input variable called scanner", I think you got that backwards ;)
12eebok 1 year ago
veryyyyyyyyyyyyyyyyyyy goooooooooooooooooooood !!!!!
u have explained this stuff in a nice and easy way,
thanx budy!
MontyTHQ 1 year ago
veryyyyyyyyyyyyyyyyyyy goooooooooooooooooooood !!!!!
u have explained this stuff in a nice and easy way,
thanx budy!!
MontyTHQ 1 year ago
veryyyyyyyyyyyyyyyyyyyyyyyyy gooooooooooooooooooooooood !!!!!!!!!!
u explained this stuff in a nice and easy way,
thanks budy!
MontyTHQ 1 year ago
thx...
deplague 1 year ago
OH man ,, Thank you very much for this fantastic simple explanation ,,
I have finally knew what is exception and do while for extra ,,
Thank you .. kind regards :)
5701524 1 year ago
Exceptions and errors are NOT the exact same thing. Firstly, because errors are unchecked, and exceptions (beside RuntimeException) are checked. Then, errors are severe problems, like OutOfMemoryError.
ibogdank 1 year ago 18
Comment removed
ibogdank 1 year ago
The class name should have the first letter capitalized - like Sample
ibogdank 1 year ago
@ibogdank It's sample.java though, if he capitalized it without changing the actual file name it would error.
LukeLeary 1 year ago
what is this editor that you are using?
lediftuna 1 year ago
@lediftuna Eclipse
LukeLeary 1 year ago
For those who read my comment from yesterday by only adding input.next(); it only checks for the first token. And then each token after seperately. So if you add what I show above and then type in "one two three four" it will say You cant do that and then Enter first num: four times before letting you enter a new value. By adding input.nextLine(); it takes everything entered as one and lets you enter a new value.
zmerz145 1 year ago
For those of you wondering how to stop it from going into an infinit loop once you enter anything other than an integer. Insert input.next(); underneath System.out.println("You cant do that"); This will reset the value of input so that it will ask for you to input the first num again.
zmerz145 1 year ago
It didn't work if I entered something other than an int.
If I enter a letter or double etc, it will run into an infinate loop of "You can't do that" 's.
Why is that if the try/catch is there to catch all errors?
GTASouthPark 1 year ago
This has been flagged as spam show
haudace 1 year ago
@haudace
damn you youtube, you don't allow spacing or new lines
System.out.println(); ffs!!!
haudace 1 year ago
you are the besssssssssssssssssssssssssssst
paz1359 1 year ago
you are the bessssssssssssssssst. :)
paz1359 1 year ago
to all the noobs you say this a good tutorial, go read a book, you are embarrassing yourselves.
the "sum" is not a sum, but thats just bad style
you dont need an arbitrary variable to break from the loop, use break.
also, in 9 minutes you can teach people to catch specific exceptions.
erjoalgo 1 year ago
@erjoalgo FINALLY SOME ONE AGREES WITH ME!
MrMelonBread 1 year ago
instead of creating an int to handle the loop, wouldnt it make more sense to use a boolean true/false?
iVuTECH 1 year ago
This is great
nabukhalaf 1 year ago
Your tutorials are great man. I think I would have failed my exams without your help.
Rebelctw 1 year ago
very interesting veido!
I tried, it was working, untill I tried to devide with -1,23
It gave me an endless loop wihout stopping at all! :) How could it be? :O
thx
kibaszottkurvajutub 1 year ago
@kibaszottkurvajutub Lmao, I guess anything with . in it does that >.>
kyle2120 1 year ago
This has been flagged as spam show
fuck you
Cammie010 1 year ago
the do-while loop only works with number. i try a letter and it went crazy. lol it kept repeating nonstop
DontComplain 1 year ago
Extend this to multiple catch statements..thanks
CamiloSanchez1979 1 year ago
thank you very much!!!
pamitha507 1 year ago
The BEST OF THE BEST TUTORIAL I've ever seen !
fireballd12 1 year ago 3
throws - throw eywords : extending the exception base class
these topics are missing, u might want to extend the exception handling topic, bucky
fsjal1 2 years ago
thanks buck but I hope to complete Exception lessons
amr0171546850 2 years ago 2
two weeks ago everything I new abt java is that it's a programming language , now thanks to you bucky AM STARTING TO PROGRAM.
2000brightman 2 years ago
try to type in 's' !
1deividas1 2 years ago 3
2:52 "some idiot tried to divide by zero"
..funny
theLichKing1 2 years ago 7
Java never looked so easy.Great tutorials.
1brixian 2 years ago
good job
hadioo7 2 years ago 23
thanks bucky that was cool. good job
jmar1982 2 years ago 2
This comment has received too many negative votes show
adfsadf
alexpea22 2 years ago
This comment has received too many negative votes show
sadfsadf
alexpea22 2 years ago
Curley brackets are called braces btw
sargentmcmuffin 2 years ago
I know each try MUST have a catch, but can a try have more than one catch? For example, if i wanted to have one message for one error, and another message for the another specific error?
Thanks!
mcgarrybd 2 years ago
sure that is a perfect question--u just ve to have specific Exceptions in each catch block n their messages message to print.
vicabelcom 2 years ago
*cough*python*cough*
joejc07 2 years ago
tnank you booky
theend1245 2 years ago
thanks bucky ol' pal
phait08 2 years ago 2
Thank You so much Bucky!
Great job.
68kaspar 2 years ago 2
Thank you!!!
llaulon 2 years ago 3
Comment removed
ILoveDogsandHarleys 2 years ago
Thanks boss! This gives me a great start on solving my problem in my program. trying to throw a exception made by me to prompt the user to only input a, b, or y. But it says the code doesn't throw an exception.
ytgodzilla 2 years ago 2
6th to comment! LOVE YOU TUTORIALS THEY ARE DA SHIT LOL
CPUhacker24 2 years ago 2
This comment has received too many negative votes show
4th
rockinghaven 2 years ago
This comment has received too many negative votes show
3rd to comment
brasilboy109 2 years ago
Well thank Einstein, you didn't need to announce it. Oh btw I just replied to you.
megamanenm 2 years ago
Thanks for this, i think i will start doing java :
Mkalola 2 years ago 23
@Mkalola Only took you 82 tutorials to decide this
MrMelonBread 1 year ago
Cool
Axelga2112 2 years ago