Java Lesson 2
Hey guys, this is Matt Kidson1.
With Java lesson two, in this Java lesson, I'm going to be showing you how to get a line from the console and assign it to a string variable. Along with doing that, I'll show you how to catch an error so if there's an error reading from the console, it'll print "fatal error." This is a very simple task, but it's harder than it should be, and there are several things you have to know in order to do it.
So, I'll create a new Java project in Xcode right here: Java Java Tool. I'll call it "console input." The code looks just the same as it did in, um, Java lesson one. We're going to add a few things to this import section.
Like I said, import imports libraries that someone has already made. There are a few others that we want to import. So we're going to import java.io.BufferedReader
. We're going to import java.io.IOException
, and we're going to import java.io.InputStreamReader
.
Um, so BufferedReader is something that reads from an InputStreamReader. IOException is an error that will happen if it fails. So you probably won't know what to do with these yet, so I'll just type the code out here for you.
So we're going to read into a string. So first I'm going to declare a string, and I'm going to make it input. By default, it's going to equal ""
because I'm going to assign it to be an empty string. So now we want to read from input.
So first, we're going to declare an InputStreamReader that is a class, and we're going to call it isr. We're going to make it a new InputStreamReader. And now here's how new
works: new
creates a new instance of a class.
Just as you might know in Terminal, whenever you're doing something, sometimes there are parameters. Like for instance, ls
, you can give it a /
after ls
or ls -l
or rm -r
or something like that. You can give it parameters. new
like class functions such as this require parameters sometimes.
So inside of this right here, we're going to do System.in
. And what System.in
is, it's an input stream. If you notice, when we do System.out
, that's an output stream. So input streams are on the way you read from, and output streams you write to.
So right here, there's an InputStreamReader called isr, and we're making it an InputStreamReader that reads from System.in
, which is like a way of input. So now we're going to make a BufferedReader, and BufferedReaders read from an InputStreamReader.
So I'll show you how to do this. I'll make it a new BufferedReader, and then in the parameters here, I'm going to put isr because you make BufferedReaders out of InputStreamReaders and InputStreamReaders out of just input streams.
So now that we have our BufferedReader, it's really easy to read into a string. We just say input = br.readLine()
, and you can read a line. There's also a plain old read that reads a character at a time, but we're going to do readLine.
So this is pretty simple right here, but sometimes there are errors and problems reading from files and from input streams. For instance, say the console gets closed while this program is running, or say there is no console; there might be an error. So we want to be able to catch that error and do something.
Java makes this easy. You can just do try {}
and then catch
in parentheses the name, the type of the exception, IOException. That's the type, and I'll make, after that, you need a variable name and then another parentheses ()
.
Right here, so if there is an IOException in any of the code between the try and the catch, it'll go to the code between the catch and the closed curly brace right here. I'll do System.out.println("fatal error")
, and that's that.
So now everything should be working properly right here. We're going to do the following: I'm going to say, "You said," space, input
, space, period. So now I'll run this — hope there's no errors.
So it's asking us for input in the console right now. So I'll type a line of input: "Hello World." "You said, Hello World." So this is more annoying than your siblings or anything; it just repeats what you said. But this is how to read input from the console, and in my opinion, it's harder than it needs to be.
Maybe in a future video where I show you how to make voids and functions, you'll learn how to make it easier to do. But for now, this is all. This is how to read input from the console.
So thanks for watching. My Kids in One for all your information and computer help. Subscribe and goodbye.