Java | Accepting User Input

Alright hey guys today we’re gonna be looking at user input I decided now is probably a good time to do input and so we can just name our class Cole because we’re just gonna write a little class that doesn’t echo and then we’ll write an echo driver so write our class because we wanted instance of echo and it.

Goes gonna take some string string input and then it’s gonna.

Say well we’ll just have it print out well we should do it in a worker method just to make sure Lorri Necco method called echo text and then it’ll take input system that of line input and then we can write an a driver echo driver.

And this is where we’ll write our public static void main there we can string our arguments and so first we need to make.

A way to get the input and then once the input has gotten we can actually just um we can repeat it back using that goer.

We can set it to the instance of that going it’ll do it from there so what do we need to do first in Java there’s a thing called scanner.

And we’re gonna call it user input that’s the variable name so this is a variable of type scanner and what a scanner do Java scanner.

Scans through things I don’t know.

Why this is great but a simple text scanner which can parse primitive types and strings using regular expressions okay so if we write it as a new scanner because you know because this is a class and this isn’t this is gonna be an object of a class we have to write a new scanner we want a new instance to assign and you’re gonna see if you just.

Put the round brackets and end it it’s gonna come up with an error because it has all of these constructors but the one we want is the input stream so inside the.
Round brackets we write the system sorry this.

Comes out in the system that in is an input stream which pulls from the console which we’ll see in just a second so now we can write our echo class because I go doesn’t have anything you can do with it instead of assigning an instance variable we’re.

Gonna write new echo just like this so this will make a new instance and construct it and then after it finishes its execution and this moves on the Java garbage collector I’ll come by and say you’re no longer being used and it’ll just dump it out of memory so you have the space so we don’t have to worry about it we can.

Say user input dot next line or we can just do next next accomplishes it just says everything that’s input into the console just work with and we’re gonna say we want to print out.

To tell them what our program does so we’re gonna say anything you input can first hear first can be echoed here.

Sorry and so and it prints it right back so we can do this again and so as you can see everything you do it’ll just repeat now.

Fascinating in Java there is such a thing as I have to.

Remember what it is printf boss I think it’s slash and it’s backslash it’s well I guess that doesn’t matter print format and we can talk about a different time simply put this is how you can receive input from inside of from inside the console of Java anything they input can be manipulated so let’s just quickly do one.

Other what’s just if you know for a fact that somebody’s gonna enter a an int we can in this case I’m just gonna upgrade it to a string but if you know somebody’s gonna enter.

An integer so let’s say ten you know print out ten as a string but you could assign the value int number equals user input dot next.

Int Java sort of knows what your email for it and then we’ll tack.

It on here number times number so we’ll square the number so ten gets you a hundred and so you can also assign the variables to the input that the users send to you and so obviously if they enter something incorrectly such as a string into an integer like this you’re gonna get some errors and so we’ll talk about errors in.

You know what the user is gonna input you can just use your scanner tell it exactly what you want it to look for so if we put a bunch of spaces on five it’ll still work cuz.

Just ignore all the white space and go for the five but as long as you know.

What they’re gonna input you can go ahead and use scanner to get them.

Posted in Programming<a href="" rel="tag">how to accept input in java</a> <a href="" rel="tag">java</a> <a href="" rel="tag">java classes</a> <a href="" rel="tag">java consol input</a> <a href="" rel="tag">java input stream</a> <a href="" rel="tag">java methods</a> <a href="" rel="tag">java objects</a> <a href="" rel="tag">java print to console</a> <a href="" rel="tag">Java programming</a> <a href="" rel="tag">java scanner</a> <a href="" rel="tag">Java user input</a> <a href="" rel="tag">user input java</a>