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

Java Lesson 3


3m read
·Nov 3, 2024

Processing might take a few minutes. Refresh later.

Hey guys, it's Matt Headson. One with Java lesson three. In this Java lesson, we're going to be making an application that multiplies two numbers.

So what it will do, it'll ask you to enter the first number, the second number, and then I'll tell you what the results of both of them multiply together is. This application includes a few things that we haven't done. The first one is turning strings into ints, and the second one is printing out numbers. Now, that's pretty straightforward, so let's just get started.

I'm going to open up my Xcode now. I'll make a new project, Java, Java Tool. I'll call it "I Add." Now, it's not actually adding, so it's a crappy name, but whatever. So now, we're going to import a few extra things. First, going to import java.io.InputStreamReader. We're going to import java.i, and we're going to import java.io.IOException.

The actual code, I'm going to say BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); etc. And now I'll say, and this is interesting, I'm going to say String num1 = ""; String num2; and the reason I'm doing this is that when you use a try-catch, any variables you declare inside of a try-catch will go away once the try-catch is over. So if I declare them out here, then if I do a try-catch and use them, then they won't be gone afterward.

So I'm going to do a try, and I'll just put out my catch right here, and this is where we're going to read input from the console. So right here, I'm going to do System.out.print("Enter num1: "); and then I'll say num1 = console.readLine(); Then my System.out.print("Enter num2: "); Now I'm going to say num2 = console.readLine();

And now, right here, I'm catching it. And if there's an error, I'm just going to print that there was an error, and another thing I'm going to show you is how to exit. To exit your program, you just do System.exit(-1); Then a number, normally that's an error code. I like to do negative one; you can do zero as well, but I like to use negative one. So there's that.

Now, under here, we're going to make int i1 = 0; int i2 = 0; and i1 is going to be the number value of num1, same with i2. So we need to use another try-catch for this. And in here, say i1 = Integer.parseInt(num1); and then we'll do the same for i2. There you go, and if there's an error here, that there's going to be more normally an error if they didn't enter a number into these strings, so we're going to say "Enter a number next time." And now I'm going to do System.exit(-1); There we go.

Now, I'm going to, under here, I'm going to say int i3 = i1 * i2; and I may have showed you in Java lesson 1 that this is how you multiply ints. And now I'm going to get rid of this and replace it with my little thing that's printing out the answer. I'm going to say num1 + " times " + num2 + " equals " + i3;

So it's going to print the value of i1, a star or a space star, and then a space, the value of i2, a space, "equals," and then a space, and then the value of i3, which is equal to i1 * i2.

So this program will multiply two inputs. So when we run it, go into the console, enter num1. So right here, if I write 1 and then if I write 2, it won't work because these are text. But if I write, say, 99, 99, it'll say 99 times 99 equals 9801, which is the correct answer.

So this is our little multiplication program that we've written. Next time, we will write one that you can either type "multiply," "divide," "subtract," or "add." So this is how to parse ints.

Um, so I hope you learned just a little something from this Java lesson. Anyway, I hope you could read everything I was doing. If you couldn't, please tell me, and if you have any questions, tell me as well because I'm here to help. I'm willing to help you.

So anyway, thanks for watching. Matt Headson, one, subscribe, and goodbye!

More Articles

View All
Improvising in Africa. Warning - GROSS - Smarter Every Day 28
Hey, it’s me, Destin. So, a lot of you may not know, because you’re new to Smarter Every Day, but I have a sister who lives in West Africa as a peace corps volunteer, and I went and helped her teach math and science. Every once in a while, I like to uploa…
This Thing is Crazy Fast - Kodak Part 3- Smarter Every Day 286
Hey, it’s me, Destin. Welcome back to Smarter Every Day. This… [KA-CHUNK, KA CHUNK] [JET ENGINE NOISES] [CHU-KUH, CHU-KUH] [KER-FLOP] [DING!] is at the Kodak Film Factory in Rochester, New York. The fact that we get to film in the plant is amazing. This i…
Example plotting corners of rectangle
The four corners of a rectangle are located at the points (11, 7), (11, 0), (2, 0), and (2, 7). Plot the four corners of the rectangle on the coordinate plane below, and they have these dots, and we can actually move these around for the four corners of o…
How Much Does The Internet Weigh?
Hey, Vsauce. Michael here. And this strawberry weighs about 50 grams, which according to Russell Seitz also happens to be the weight of the entire Internet. What does that mean? I mean, the Internet is a gigantic place and how do you measure information? …
Catch of the Week - Nine Foot Monster | Wicked Tuna: Outer Banks
[Applause] [Music] Yeah, that looks like a p. Look at this, bro. It’s definitely weird to be fishing this close to shore; there’s no doubt about it. But as you can see, there’s a guy yanking on one right next to us, and we’re seeing plenty of bird life w…
The Constant Fear of Driving While Black | National Geographic
I have this a lot of police of about four times in the last sixty days. A total of five times I’ve been probably more than 20 times. It’s more times than I care to remember. But what you do know is how a very familiar feeling comes each time I’m stopped. …