Java Programming Tutorial 10 - Scanner Input

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody this video we're gonna be talking a little bit more about how to get user input in the previous examples all we did was get a string input but now I want to talk about how to get input of these various types so this video is sponsored by pram and if you don't know cramp is a peer-to-peer interviewing platform where you can practice your technical interviews as well as your behavioral interviews cramp is a great resource for developers like you if you're hoping to get a job in the industry or if you're just hoping to solidify your interview skills cramp is the way to go so check them out guys I'll leave a link for you guys in the description they're super awesome you definitely want check them out alright so the previous video we talked about all these primitive values and I'm gonna get rid of this just so we can have a clean sleep now if you guys remember how to get user input what you need to do is you need to create a scanner and then you can name it whatever you want I'm gonna name it scanner with a lowercase s and set that equal to a new scanner which we'll call the scanner constructor and give us a new scanner object inside of the parentheses you need to pass system dot in now to get a new line all we have to do is say scanner dot and then next and there's all kinds of different options in here the one we did originally was next line this will return a string so when you press the dot and then type out the function name or the method name by the way function and method are basically the same thing just the only difference is a method is attached to an object and since we're in an object-oriented programming language these are technically called methods but enough of that if you look at this you can see a little bit more about what the method does this one's not super clear what's going on but basically this is going to get us a string if you look at a different one such as next int it's a little bit more clear the int scanned from the input so let's start with next line and just see how that works so what this is going to do is it's going to get a line and return it and what that means is it's going to give us back an output and we need to do something with that output so typically we're going to store that in a variable so we could say string x equals now we can output that with system dot out dot print line it really super bothers me that this one's print line with like the lowercase Ln and this one's lined with a capital L forward but what are you gonna do we can pass X into here and now let's run it and then I'll expect an input and then it will output what we just put in all right so that's how you do it with strings now let's try some other data let's say int y equals next let's run that and then what we could do is we could output that as well and then my age is five it prints five the cool thing here is that this is typed as an integer typed as in datatype not like typing on a keyboard and what that means is we can use that inside of an expression so I could say in a equals y plus 10 for example and we're gonna talk a bit more about mathematical expressions but for now let's just go with this and I'm gonna comment out this one so it doesn't keep asking me for input all right so now we can just pass in 20 and we need to print the new variable a alright so you pass in 20 and it prints the original one then it adds Y and 10 and assigns that to a so now a contains 30 and it prints 30 now if you want to get any of the other data types there are different methods for that so let's go scanner dot next and now let's just look at some of these so one of these is next big decimal and next to big integer these are two data types we never talked about because they're actually classes and we've only really dived into primitive types but if you guys remember the float and double datatypes they allow decimal values well this big decimal also allows decimal values so you could store something like 10.1 but the difference between big decimal and float and double is that big decimal is trustworthy when it comes to precision so float and double are known as floating point numbers and this is a fixed point number so if you're intending on working with something like money where you need it to be exact you're definitely going to want to use a big decimal and that would look something like this so you could say bigdecimal and we can say money and say oops money instead equal to scanner dot next big decimal this method here is going to return a big decimal so I need to store inside of a big decimal which requires us to import this right here java dot math dot big decimal so that's how you could get a big decimal from the user there was also one for big integer which is slightly different than a normal integer so if that's something you guys want to learn more about you can research that anyways let's look at some of the other ones so next boolean that can be used to get a boolean value and it says here that it'll throw an exception which is just an error if it cannot be translated into a valid boolean value so let's try it and I'm gonna get rid of all this stuff here and we're going to store this in a boolean variable like this we're going to output it to let's put the value true and it works we put the value false and it works put something like 27 it gets an error so basically through an exception meaning that our program was not able to execute because there's an input mismatch alright let's see what else there is and then there's this next byte with a radix so what that is is the base of the number so we count in base 10 but if you're looking to use hexadecimal or octal for example you could check that out there's also one for the next integer with the radix and next long so if you're working with larger exa decimal octal or even binary or whatever the base is if you're working with those you want to use one of these with this radix here so let's just try this out let's say we want to get an integer using this radix and let's say we want to pass in eight and then let's store this back in an integer we'll just call it X and we'll sit the system out this X okay and now let's run this so let's just put in the value one zero and this input is an octal base eight and what that means is this is going to be interpreted as eight and decimal so when we output it it's outputting in decimal we get to value ake if that makes no sense to you guys that's fine because we didn't really talk about a much you can definitely go check out my videos on octal hexadecimal and by which will clear all that up for you but most of the time you're not going to need that you're just gonna be using the one with no arguments and now it'll work like decimal say I put in ten it gives us ten and then of course there's ones for double and Flo and long and short and all the different variations so that covers how to get input of different types another thing you may want to know is just about casting we talked a little bit about this so for example we might be getting an integer but then we might cast that by storing it into a double in this situation it's automatically behind the scenes or implicitly converted to a double but in some scenarios let's say we got next double and we're storing that in an integer we might need to cast that manually by like the by using this parentheses int so in a situation if we run this and we put in the value thirty point five or even thirty point nine it should just print 30 and there you go you get 30 so that's how to get input of different types hopefully that was helpful for you guys please be sure to subscribe if you've enjoyed the series definitely helpful to me and be sure to check out the description for some good links for you guys thanks guys see in the next one [Music]
Info
Channel: Caleb Curry
Views: 106,201
Rating: 4.8913293 out of 5
Keywords: java programming, java, programming, scanner, input, beginner, code, coding, how to, java tutorial for beginners, caleb, curry, calebthevideomaker2, java scanner, java tutorial, scanner class
Id: rqZ0Cp9Omp8
Channel Id: undefined
Length: 8min 4sec (484 seconds)
Published: Wed Nov 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.