yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java Lesson 2


4m read
·Nov 3, 2024

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.

More Articles

View All
Without academic freedom, we might never see the truth. Here’s why. | Nicholas Christakis
So when we think about academic freedom, we might ask the question, “Well, what’s so special about academia?” Why might we have special rules of communication and openness within academia? I think in approaching this, one might think about other parts of…
2016 Personality Lecture 03: Mythological Elements of the Life Story -- and Initiation
So, a couple of things I want to tell you about today. Many things I want to tell you about. I want to tell you. Uhm, I want to give you a schema that is going to enable you to understand. I think it will enable you to understand stories. Like the one I …
How to Apply and Succeed at Y Combinator by Dalton Caldwell
Okay, what we’re going to talk about today is how to apply and succeed at YC. In my capacity as the head of admissions, I go around the world, and I give talks, and I meet folks that are applying, and I answer lots of questions. So I think I have a decent…
The best way to apologize (according to science)
Over the years, people have come up with some truly awful apologies. From classic non-apologies to evasive excuses, and flimsy corporate promises, it’s all too easy to give a bad apology. But researchers have found that good apologies generally share cert…
How Much $ You Need To Live Off Dividends (FOREVER)
What’s up? Grandma’s guys here, so let’s talk about how much you really need invested to live entirely off the dividends. I’ve tried just about every passive income idea that you could think of—from side hustles, real estate, intentions, marketing progra…
A Park Reborn: Charging Elephants | Nat Geo Live
( intro music ) There used to be more than 4,000 elephants in the greater Gorongosa area. And during the war, most of those elephants were killed. Hungry soldiers ate their meat. And traded their ivory for guns and ammunition. When it was all over there w…