Java/JavaScript/React JS training


language: EN

                  WEBVTT
I see some computers up, but I can't tell if people are interacting with them.
Mine is, it's been up since yesterday.
I know a lot of it.
Yeah, I'm writing right now one of my scripts.
Nice.
All right.
All right.
Let's see.
I think I'm going to go ahead and get started.
So the first thing I should do is press record.
And you know what?
Even before I do that, I'm just going to send Maria an email and let her know that we are
up and running and doing well.
Okay.
Now I will press record.
Please ask the host to take the progression record.
Hey, hey, hey.
Who's the host? Probably Maria.
Let me just double check and see if anyone else is labeled as the host here.
Let's see.
Maria's on mute.
Good.
Yeah, I'm just trying to get a hold of Maria real quick so that she can give me permission
to record this.
Where can we find these recordings?
It's a really good question.
Is that some of your questions?
I don't know the answer to that question.
I can figure that out.
Andrea, if you go to the old meeting and you kind of right click on it, there's a recording
tab that comes up.
When you say old meeting, what do you mean?
Like yesterday's meeting, if you wanted to look at yesterday's meeting, inside Zoom,
you go to that meeting and you find it.
Okay, inside Zoom.
Okay.
Thank you.
Yeah, inside Zoom.
Yeah.
All right.
Looks like we've got Noble Prague, Texas, the host.
Sounds like Maria's helping us out.
Hello.
Hey, Maria.
Thank you.
That's me.
I thought you will have that option.
Okay.
Can you check if you can?
Yeah, it says I'm the host now.
So let's see.
I will record to the cloud.
Recording in progress.
All right.
This looks good.
Let's see.
Yeah, I don't actually know.
Can you talk me through?
Oh, let me share my screen.
Can you talk me through where you're seeing these?
Finding the recording?
Yeah, totally.
All right.
I found it the other day.
Okay.
Where's my Zoom window?
Hey, you're overplaying me.
Yeah.
You're completely blacking me out.
Okay.
Well, we can figure that out later.
It's pretty easy.
I found them right away.
Okay.
I'll Google it.
Sign into the Zoom web portal.
Click account management, recording.
Can I jump in?
I'm sorry.
Are you looking for the recording from yesterday?
Yes.
Okay.
So you won't have it.
I need to release those recordings.
I will have to just send those documents to Polarina.
Gotcha.
All right.
Thank you.
All right.
Great.
Well, let's get started and I'll show you a little bit of something that I've created
to help us with problems like this.
So I created this webpage, calling it sort of a course webpage.
And we've got some handy...
Let me go ahead and put a link to this.
It's the same URL that we used yesterday.
But I recommend that you keep track of this.
Because this will let us access everything.
So what I've put up here is a link to the etherpad.
So that was what we ended yesterday with a few links to things.
Like we have a link to the IntelliJ tool.
Now we've got a link to one program I created.
I actually also created a Slack for us.
Has everyone...
Has anyone not used Slack to chat?
We do not.
Yeah, I haven't.
OK.
I mean not.
Let me show you what it...
Get the hell out of here.
What's that?
Not me.
OK.
Let me just show you what it looks like.
Maybe we use this...
Slack is from Salesforce, right?
Sorry, I can't hear you.
I think that isn't Slack from Salesforce.
Yeah, I'm not sure who...
Look at Norris.
Who built it.
Yeah, it's a useful chatting tool.
The only reason that I use this...
So here's what it looks like.
You don't have to use this at all.
I think it'll be a convenient way for us to ask questions outside of class time.
If you have something on your mind, I can hop on and you can chat here.
I can chat.
It should be an easy way for us to share files directly if that becomes a problem.
Most of this stuff on Dad Desktop will give us what we need, but I thought we might as
deal with user input and read files.
After that, I'm going to talk about object-oriented programming in Java, so we'll get a glimpse
of how to create our own classes and how to have our classes encapsulate pieces of data
and how to add methods to modify that data.
And I want to talk about the Java Collections Framework.
So the Java Collections Framework gives us more access to more of the built-in data structures
inside Java.
So far, we've seen primitive data structures or primitive values like integers and doubles
and Booleans and single letters, the cars.
And we've seen some objects like strings and arrays.
What we're going to look at today is lists and maps, which will let us create some more
powerful programs.
So let's get into it.
This is Java style.
Back on the course webpage, I put a link in today's resources to a Google Java style guide.
That thing's pretty dry, but if we take a look at this Google style guide, it just lists
just the different ways that people write Java.
So there's a difference between syntactically correct Java, where that just means you have
to have the correct number of curly braces or semicolons, versus...
So we can say there's syntactically correct Java, but there's also Java with good handwriting.
I mean, like, handwritten stuff.
So let's look at a few conventions just to make sure that we're all writing our code
the same way.
Probably the easiest...
I'll call it a mistake people make when they're writing Java is they will drop their brackets
below a line.
This is something especially common with C programmers.
Again, this won't change the behavior of your program, but it's just nicer if we can all
agree that we're going to write stuff the same way.
Oops, I'm accidentally scrolling.
So whenever we're writing anything with curly braces, we tend to keep that curly brace on
the same line as a for loop versus putting it below the line.
Another thing that I saw some people doing yesterday is omitting curly braces.
So it is totally syntactically correct.
Well, legal.
Java will let you write an if statement, and it will let you omit a curly brace, and it
will execute a single line.
But there's really no reason to do this.
So I'm going to encourage us to always put the curly braces, even if it's just a single
line of code.
A lot of these style guides help us keep our program syntactically correct.
So yesterday we got a little taste of IntelliJ and how it suggests different code to write
or it will auto format your code.
If we write our code in a very clean style, IntelliJ will work better with us.
If your code is written in not a clean style, it's hard to see what you might have wrong
in the program.
Another style that Java conforms to is capitalization.
So in Java, whenever we have a file name, the file name is always capitalized, like
capital M, my program.
And if you have another word, we capitalize that word.
Similar, if we have a class name, that should be capitalized.
Furthermore, the class name specifically must match the name of the file that it's in.
So that's not even a style thing.
That's just a straight up rule.
And when it comes to variable names or functions, Java uses something called camel case, where
we start lowercase and every word after that is capitalized.
So here I've got a variable and let's see, it has a lowercase s and it says saying of
the day.
So we capitalize of the day.
And same with a function name down here.
We start with a lowercase and we capitalize the next word there.
So this stuff might seem a little bit pedantic, but it's just nice to agree on that.
Let's see, here is a question.
I always talk about capitalization and not capitalization.
Does anyone want to take a guess why the types don't seem to agree with these rules of capitalization?
So my question to you is, how come int is not capitalized, whereas string is capitalized?
Int is primitive.
That is exactly correct.
So int is a primitive value.
So we've got int, long, float, double, Boolean, car.
That means that this primitive value is not a class.
It is not a object.
And that is in direct contrast to string.
So in Java, the string data type actually came from something that is a class.
So because this is a primitive value, the type of int just happens to not be capitalized.
Let's see, I think that's about it for stylistic stuff.
Any questions or comments about Java style so far?
You can disagree with it for sure.
You have my and an uppercase.
M is capitalized and P is capitalized in my program.
Would only the first character have to be capitalized or is that just a convention?
The convention is whenever you have multiple words, you can squish them together and we
like to capitalize every time a new word starts.
So it's a capital M because this is a class name.
And since, you know, if this were English, we'd be writing my space program.
But yeah, we definitely capitalize the P in program just because it's another word, makes
it easier to read.
Do you allow underscores in class names and file names?
That is a good question.
Although Java will allow underscores, stylistically, we should not put them there.
It is not commonly accepted to have underscores in Java names.
That would be more of like a Python style.
Let me just show you one glimpse of something I referred to, the Google style guide.
So this is awfully dry.
But let me make my text a little bit bigger here.
Some things that they talk about.
OK, so they're going to have some rules about file names.
They've got some rules about formatting.
Here's some here's some good things.
OK, they're going to talk about braces.
They're probably going to say, you know, don't put drop braces.
Keep your braces on the same line.
Here's another good one.
A column limit of 100.
So they're going to mention there if you ever have a really, really long line of code, they're
saying that, hey, it's probably better to try and keep your your lines of code a little
bit shorter.
So let's just look at some of these.
We're not going to look at this forever, but I'm just want to show it to you to show that
it exists.
Let's see.
So here it says, you know, optional braces.
So braces are used with if else for do and while statements, even when the body is empty
or contains a single statement.
So that's what I that's what I was saying earlier.
It's like Java will allow you to emit these braces, but you just shouldn't.
Let's see what else it talks about.
Looks like here they don't really care if you have an empty block.
Maybe you can write it very succinctly with curly braces together or or break it apart.
Not a big deal.
They talk about indentation.
So it looks like they say you should indent with two spaces.
Probably a lot of our editors are set up to indent with four spaces.
That's not too big of a deal.
That's about as much as I want to look at here.
If you got a literal that makes you go over 100 out of your rabbit.
Yeah, let's check that out.
That's a good question.
Let's see if you have a literal like a string literal.
Well, there's always exceptions to rules.
And if you got just one really long line of text, it's probably not the worst thing in
the world to just leave it as a long line of text.
Hey, dog, please be quiet.
Probably I've got a better answer for you there.
This dog's just going to keep barking.
Public static void main string.
String really long line is equal to.
Let's go ahead and get some lorem Eepsom generator.
This is just a placeholder text.
So if we had a long line of text, come on, generate it.
What kind of generate it?
This will generate just random text.
So this is definitely a long line of text.
I think this little line here is kind of showing us that might be like the 100 character line.
I guess this one is I'm actually looking down here.
This will show us the same line three character count one twenty one.
So I guess this is set to one twenty.
I'm not going to get too particular.
Here's the actual answer to your question.
You can put a break here and you can write something like this.
And the carriage return you enter.
I am actually the plus plus was the continuation sign.
Yeah, I'm actually this is actually concatenating a bunch of strings together.
So technically there's some performance impacts here, but I think it's a sufficient answer.
I think really long lines like this are generally going to be rare.
Yeah, but if you get a piece of code and you're going to go over a hundred, then the plus sign is says, oh,
the next thing that comes with a double quote is going to be a continuation of this.
Well, this is literal string concatenation.
So this is like, uh, who, you know, let's see some string string bar, some other string string both together.
This is all equivalent.
This is like it's actually pretty in this out or it's actually concatenating them together.
So it's it's it's like it's actually performing a it's actually performing a concatenation.
Oh, yeah. Yep.
And it's just that that's a common problem I run into.
Have to do a lot of string matching.
And yeah, I get you know, I run into that a lot.
Gotcha.
Let me let me look up one thing.
Let's see Java multiline string.
If you put a slash in it.
Well, a slash in is actually going to here.
Let's let's, uh, but it will introduce a line break.
Exactly.
Yeah.
Yeah.
Here, let me let me execute this program.
Um, here, let's see line breaks.
So if you do, yeah, slash and we'll introduce an actual line break.
Um, I'm just going to delete this.
You should remove the plus the quote and the plus.
Uh, it's only when you output it.
So it doesn't help that the editor because you should you should join the first and the
second line like, um, what is it 16 and 17 together with the slash and, but the slash
and will only be useful when you output it.
Uh, yeah, let's run this and see what it looks like.
So here, two short strings, we added them together and they get printed together.
Some string, some other string.
In this case, without the slash ends, uh, when it gets printed out, it, it appears still
on one huge line.
And here, if we add, uh, line breaks, then those line breaks are preserved when we print
it out.
Uh, I think I just Googled this.
No, that's I there's actually a good answer to your question.
Um, I think you can put in, uh, three.
You do triple quotes and you can put some text in here.
Let's see how this looks.
Neil, you're asking for the editor, right?
So view it in the editor.
Hey, the script, you got to execute it.
Right.
It's like, you could imagine having a bunch of SQL, um, and you have, or yeah, any, any
crazy, uh, dang it.
Come on.
That's exactly where I'm going.
We do a lot of embedded SQL.
Yeah, totally.
Um, I really thought for sure.
Getting the line separator.
Yeah.
Yeah.
I, uh, I guess I don't have a great answer for this question.
I've seen, I've used programs like programming languages like Ruby or, or Python.
And sometimes those, I know Ruby for sure has, has a fancy way to preserve this stuff.
But, uh, yeah, I guess I don't have a good answer off the top of my head.
I'll write a note to see about getting that.
Um, well, okay.
This is, this is the answer actually.
Yeah.
Okay.
So the actual answer that I have that's pragmatic is, is definitely this.
You use these plus signs and, uh, as we see in the output right here, uh, this preserves
this, this preserves the, uh, um, I guess the format of the, of the text, you know,
if, if you want, if you wanted them to appear, uh, narrow in the editor, but you wanted them
to stay along the line, you can, uh, manually break it up and append the strings together
with the addition operator or, uh, with the new lines, you know, this is actually gonna
break it up.
Um, if you actually want it to be broken up when it comes back out, does that make sense?
Yeah.
I was just thinking a little further in my head, you know, I'm constantly embedding,
uh, my SQL.
Yeah.
A lot of my programs.
Yeah, totally.
So here, here we could have something like string SQL equals select, select, uh, about
street, city, state zip code from, uh, what else?
Okay.
We'd say that from like addresses, maybe how about, uh, customer ID from addresses,
join.
Uh, what else would a customer have?
Maybe they would have, uh, sales.
I don't know.
Yeah, that's a good idea.
Uh, join.
Okay.
I was just saying that where I run into that all the time is when I'm embedding SQL.
Right.
So, I mean, that, that would look something like, like this, you know, this is like, I
don't even know if this is exactly correct.
Um, you know, maybe we'd have to say, how about like, okay, bank, uh, account number.
Maybe we'd say, uh, savings and checking.
So here's like a long line of SQL.
Um, well, the way that I would probably do this is, uh, put a line break here and make
this a string.
I would probably write it somehow like that.
One thing to be careful about here is we don't have any spaces.
So here let's, uh, let's print this out and see what it looks like.
System out, printlin, SQL.
Let me put a blank, uh, print statement here so that we get some nice spacing.
The problem with this is, is going to be, uh, one, uh, it doesn't preserve those line
breaks and two, you gotta watch out.
Is this large enough to see?
Do you see how the from that parentheses?
Yep.
So whatever you do this, you'd have to be careful to, uh, either insert the line breaks
manually or, uh, like up here.
That's the reason I put the spaces there.
So you just gotta be aware of that stuff.
Yeah, no, I just, uh, I run into this problem every day.
Nice.
Yep.
It's unfortunately, uh, Javan probably doesn't have any real silver bullets for this solution
for this problem.
All right.
That's probably enough about style.
And that was a, I like that detour.
Um, I definitely wanted to talk about Boolean expressions.
I think I did not give you a formal introduction to these, uh, logical operators.
Um, so with Boolean expressions, uh, and primitive values like an int or a double or a character,
uh, we can use, uh, two ampersands for a logical and operator, or we can use, uh, two pipes
for a logical or operator.
Uh, we can use the exclamation mark to negate something.
And we use, uh, two equals.
Uh, I often say, uh, double equals.
Oh, this is funny.
I'm missing an L right here that should say double equals.
And Java definitely supports, uh, the less than operator, less than or equal to operator,
uh, greater than or equal to operator and the greater than operator.
And you can, uh, create some Boolean values.
You know, here I'm storing things in a Boolean.
Um, you can put them inside of a, uh, if statement too.
But here I'm saying like, okay, let's see if a business is open.
Uh, let's see if the hour, uh, you know, I'm kind of just making this stuff up, but I said
the hour is 900, uh, and some military time, like an hour is, uh, greater than, less than
1700 o'clock.
Now there is a difference, uh, between primitive values and objects in Java.
Uh, objects like strings, you cannot use a equals equals method, uh, for an object.
So if you're using something like a string and you want to compare a string to something
else, uh, you should actually use a method that exists on the strings called the string
equals method.
So, uh, because objects are just more complex than primitive values, uh, you, you have to
use a method that's, uh, going to look at that object a little bit more carefully to
figure out if it's the same.
So in this example, I've got, uh, two strings and we can say, uh, if the first, I changed
the variable names, um, and I'm missing a, a parentheses too.
This is what happens when you just write text and you're not actually executing it.
Uh, but yeah, uh, we use the first string and you say dot equals and you can compare
it to another string.
And then I've got an example of the, uh, the negation operator.
Uh, a common mistake people make is taking something like a string and another string
and comparing them with, uh, the double equals equality.
Let me show you what that looks like.
Just a quick, uh, string equality.
Hey, Steve, sorry.
I don't mean to cut you off.
Um, is it possible to make your font a little larger?
Cause, um, I'm working on a laptop and it's really small and I'm looking at it.
Yes, absolutely.
Thank you.
I anticipated that and I appreciate you saying something.
Thank you.
Uh, let's see.
It's going to be, it's going to be under appearance.
And then, uh, there's a percentage that you can change from whatever, you know, 100 to
125 or there you go.
Worth the zoom.
Let's try this.
How's that?
That's better.
Thank you.
Yep.
Is that sufficient?
Yes.
I don't want just better.
I want sufficient.
Okay, great.
All right.
So let's see what's going on here.
Let's say a string.
I'm going to call it drink one is coffee.
How about string drink two is a soda string drink three.
Yeah, let's get some beer in there.
So now let's, let's print out if we can just compare these things.
If drink one is like equal to drink two.
And you know what?
I think I need a.
Let's let's just see what happens here.
Let's see if drink one is equal to drink one.
So let's see.
Let's put a note here.
Do not use equal to drink one.
Equal equal to compare objects.
Use dot equal.
Okay, drink one drink two.
Okay, so.
I may have missed it.
What's the difference between a double equal and a single equal again?
A double a double equals most often used for assignment.
Whereas a double equal is used for comparison.
So yeah, here we're saying this is a variable.
The variable is being assigned to the value of coffee.
And then here we're comparing the two.
Yeah, yeah, there's some old programmer Dykstra or something.
He famously hates the equal sign.
He's like, you should not use equals for assignment.
He hates it.
He prefers something like colon equal to to make it more clear that it's not like a comparison.
Let's see what's going on here.
Is drink one the same as drink one?
We'll print this out.
Is drink one the same as here?
We'll say drink one equal to drink one about is drink one equal to drink two.
I need to catenate that.
And what about this is drink one equal to drink four.
All right, let's see what happens.
So let's compare this to.
I can dot equals.
OK, let's see what happens over here.
System out, printlin.
Let's run this.
I want to run that again.
I'm a little bit confused by this.
I don't see any of the equality stuff here.
The second batch is expecting performing how I expect, but I'm a little bit confused why I don't see this.
I guess I can see what IntelliJ is trying to tell me.
Well, we're appending a string to a Boolean value.
Let's do something.
Let's do something weird for a second.
Sorry for the hiccup.
But the first time you ran it, you just said does drink one equal to right?
You didn't put a period in front of the equals and it didn't display.
Yeah, I guess.
Yeah, at first I.
I was writing just these.
But if you just instead of the equal to double equal to if you just said equal to it, even that is not working.
It didn't.
I think the first time you did it, it just said false also.
So why is it not taking the in within the codes?
If you remove the equal in the first one for that line, you're on if you just replace the double equals to with.
Like that.
No, before the one within the codes.
Well, that's just a string.
So it that that's just text that we're reading.
So that shouldn't matter.
But it didn't print it.
Yes, I agree that it didn't print it.
I'm not sure why I didn't print it.
I think it's can we run it now with the single equal to.
Yeah, absolutely.
I think it's going to remain just false false false false.
Oh, it didn't print the first one.
Yeah.
Here, let me just look something up real quick.
Java string Boolean concatenation.
Hmm.
String plus new Boolean Boolean to string.
That's that's awfully weird.
Let's just keep this easy for ourselves.
Well, here, let me again, I apologize for the hiccup, but now I'm a little bit curious.
What happens if we just print?
Let's see what happens if we just print this.
That's true.
OK, what if we put an X in there?
That one prints.
What the hell?
What about?
What if we.
I'm putting parentheses around it.
To try.
I think I have some idea of what's going on now.
Let's let me run this first.
OK, great.
Yeah, I think we had a an order of operations problem where.
What was happening?
I think what was happening is it was basically saying Boolean flu is equal to.
So this is this is the mistake that was happening without the parentheses.
It was taking the parentheses and the numbers and the numbers and the numbers and the numbers.
So the parentheses, it was taking this string and it was appending it to this string.
And then it was asking if that entire string was equal to this string.
So that's why it was printing out just.
The false and the true stuff.
Does that does that make sense?
Sorry for the confusing error there.
Anyways, we don't know.
OK, because of the parentheses.
Yes, yes.
I'll just leave it.
I'll leave a note here.
Oops.
Without the parentheses, the order of operations was messed up.
All right, now we can get back to the.
The actual.
This doesn't illustrate my point at all.
Maybe get out of the weeds here.
OK, this is a little bit ridiculous, but.
The main point here is to is to not use the the double equals comparison.
What I just did here like this looks extremely strange, but.
We have drink one is equal to the string coffee.
Drink four.
I had to say new string cough and new string fee.
Let's take this step by step.
Do we agree that drink four and drink one essentially contain the same string?
Let me prove that to you.
It looks like it.
Oh, yeah, let me prove to you that they appear the same.
So here in the output, we see that drink one is coffee drink for his coffee.
But if we use the double equals comparison, it says drink one.
Yeah, that's double equals itself.
That's true.
We see drink one.
OK, does drink one double equal drink two?
No, and that's fine because drink one was coffee and drink two was soda.
But I went out of my way to.
Create drink four in sort of a strange way.
And we can see that drink one and drink four.
They have the same value, so they both have.
The value coffee.
But if we use the double equal quality, Java tells us that these are not the these are not equal.
So the reason for that is because.
Objects in Java are just a little bit more complex than primitive values.
So a primitive value like the integer 90.
Well, if you compare an integer to another integer, it's basically pretty easy for a computer to see that 90 and 90 are the same.
They're just actually the same value.
But when we get to things like strings, the computer needs to do more work to see that these strings are actually equivalent.
When I say more work, it needs to look inside the object and look at the characters and it should compare these character by character.
So when we use the equals equals operator, it is only looking to see if like a primitive value is exactly identical to another primitive value, or it's looking to see if two objects are in fact actually the same object.
Not that the objects contain the same data, but that they're actually the same object.
Let me show you one more thing.
Let's see.
Let's see Java print object ID.
Trying to get it.
Let's see if I can get it.
All right, maybe a hash code. I don't know.
Oh, I thought there was a way to get a Java ID Java.
Print object memory location.
I guess people are saying hash code.
Let's see what happens.
Yeah, okay, I'm not gonna do that.
The main point here is, don't use double equals for for for strings, use use the actual dot equals method.
I'm trying to illustrate that there's there's different instances of these string objects. We'll see that more once we actually create our own things.
A string is more complicated than the numeric value.
Yes, absolutely.
A lot of things go through with a string. And so to say that a double equal is a very simple comparison.
Yes, exactly. Yep.
The equals goes in and looks at what what you're looking at and then determine how it should be compared.
Yeah, the Yep, I like I like your your simplicity of just saying the double equals looks at at just a very, very simple level.
You use double equals for integers.
Yes, definitely.
Here, let's say like we've got x x equals 99.
Why y equals 99 in ZZ equals 42.
You'll see that actually x x.
The fact that it's a primitive value x x doesn't even have.
Like these are these are the things that IntelliJ is suggesting, which are all actually very, very strange.
I'm actually not even sure what most of these suggestions are.
But the real fact of the matter is that it's not even possible.
So the fact that x x is an integer, it is such a primitive value that it does not even have that dot equals method attached to it.
I'm going to leave a comment here and I'll say primitive.
When you get away with the double equals, it's probably quicker.
Yeah, it is more performant for sure.
So we're online 20 and where do you want the parentheses?
Where you at right now?
Like this?
Yes.
That that's just surrounding that itself.
And it's it's entirely the same.
Yep.
Those are those are equivalent.
Yeah, that definitely works.
All right.
That's about it for Boolean stuff.
Let's check out some more exciting things.
Reading files with a scanner.
All right.
This is where we're going to get some more some more meat.
It looks like someone's chatting.
That was just Maria saying everything set up and have a good day.
All right.
Thanks for it.
All right.
Let's revisit the scanner.
I don't think we looked at this too much yesterday.
We opened up some files that had some examples where we had some some user input.
Let's let's look at the scanner.
So the scanner is a class that's built into Java, and it will do things like read files, or it will read user input.
This is a piece of code that shows you a traditional way to interact with the scanner in a file.
So basically we will give a the name and location of a file and we can use an object called a file object to actually get a reference to that file.
This is totally a typo.
I should fix this.
That should say that's a new scanner on the file.
And I'm noticing I've got a typo here.
That should be a parentheses.
We surround this entire process with a try catch block.
So it's totally possible that we could give a wrong
name of a file and that file might not even exist.
So if if we say if we gave it a incorrect file name, and we tried to have the Java open up that file and create a scanner for it.
That might cause an error.
And we can catch specific errors, like this is a file not found exception.
And if we catch this error, we kind of control how our program crashes.
So maybe we can print out a nice error and just tell the user, sorry, there's an error reading the file, or maybe if we were in a more sophisticated program we could have someone enter a different file name.
So, I have a good file that we can play with today.
So I want to show you how to get this set up to read a file, and then we'll do a little lab break and give you all a chance to interact with this file.
The file that I am going to use today is called Titanic CSV.
And let me go ahead and do this in DAW desktop just to be fair and we'll all be on Windows.
Let me open up my browser. So I'm just going to run through this once and then we'll take a break and get you all set up so that you can do this as well.
Steven.com slash Java.
I'm going to right click on this Titanic file, and I'm going to save the link as something.
And it's in my downloads folder.
I think I'm going to open up my file explorer and go to downloads, and I've got a reference set file here.
Now I'm going to open up IntelliJ.
You know what I'm going to do just for the sake of keeping things easy.
I want to make an entirely new project.
And I'm going to call this Titanic scanner.
Looks like I'm going to have it exist in the desktop Java directory.
The reason I'm making a new project is we've got so many other files open.
I don't want an error in another file to screw up what I'm doing in this file.
We can.
Looks like this has created us a program called main.
I'm going to kind of delete everything in here.
And I want to.
Go to my downloads folder, and I'm going to add this Titanic text file right next to this program.
I tried to add it in here.
Let's see.
Did that work?
It asks me if I want to move this file.
Yeah, I guess so.
There we go.
So this is a CSV to comma separated value file.
And what this what this file is, is it's data from Titanic.
So it's actual historical data about people's names.
And it's got like their gender and their age.
And we've got these columns like their ticket, how much they paid.
Looks like it has a field for embark.
Like you can see if someone was they came from Paris or something.
So let's create a Java program that can read this file in.
And then we can find some interesting questions like, oh, they've got their passenger ID.
We can see if someone survived the disaster or not.
But let's write a program where we can answer some questions like we can ask how many males were on the ship, how many females were on the ship.
We can ask how much money did the Titanic make?
We can ask like for the fair.
We can make some calculations like what was the average fair cost?
So let's let's just get the basic bones of a program created so that we can actually interact with this file.
I'm sorry, Steve. Is it OK if you make a little bigger?
Of course. Oh, yeah.
Let's go to settings.
Thank you.
How do we import this CSV into our desktop?
Um. Yeah, you'll have to go to steven.com slash Java and you'll have to right click on this file and save it.
And I can walk you through that.
I just want to I want to get a simple example going first, and then we can all break individually and I'll give the instructions again while everyone's doing it.
How does that sound?
OK.
Oh, man, my keyboard seems to be messed up a little bit.
Let me refresh my browser and see if I can get this to work a little bit better.
No, I can't.
There we go. I had to press a button.
OK, so the.
Basic pattern to get this to work is we need to give it a file name.
We need to make a file and we need to make a scanner.
So I'm going to refer to the file name with a string.
I'm going to say the name of this file is Titanic CSV.
Now I need a reference to the actual file.
Notice that this.
This thing file is an object.
It's built into Java.
IntelliJ right now is telling me that I need to import this.
So I am I think I'm going to write my line of code first and say it is a new file and we can give it this file name.
And now if I hover over here.
It's trying to get me to do a keyboard shortcut, but I'm trying to be.
All right, I'll do it. I'll press all enter.
That was not good.
Yeah, I was trying to get this to appear.
So I want to say import.
So we haven't used imports yet.
When we import things in Java, we write it at the very top of the file.
And we're going to need to import this scanner class to someone yesterday said Java was very repetitive.
This is probably the most repetitive line in Java.
You'll see we'll say scanner scanner equals new scanner.
And we give it this file.
IntelliJ was nice to me.
It automatically imported the scanner up here.
There is another thing that it's going to tell me here.
It's telling me that there's an error that we haven't handled.
So this says I have an unhandled exception.
It's saying that, hey, if you create a scanner on this file, it's possible that that file might not even exist.
So it's trying to get us to account for this.
It's going if we click this button, it's going to modify our main method.
I'll show you what this does.
So it threw it basically annotated our function up here.
It added something on our main function.
It says, hey, this function throws a file not found exception.
That's actually a really tacky way to deal with this.
So I'm going to delete that.
I prefer to handle this a different way.
Let's see if it.
So another option it will give us here is it says we can surround this with a try catch.
So I'm going to choose that option.
I just like us to.
Yeah, let me let me show you.
Let's let's do it.
Let's do it the other way first.
Just just so you see what what we're actually doing.
And I'm going to call this Titanic.
Not exist.
OK, so let's let's try and run this program right now.
I'm going to run it.
My prediction is that this will not even compile.
So this is what I think we've been lucky and most of our programs have actually compiled so far.
That's probably not true with a bunch of syntax errors from yesterday.
But Java won't even attempt to execute this program because it is able to introspect.
And it knows that this thing can go wrong and it forces us to deal with it.
So the compiler error, it says that we have an exception file not found exception.
And it says we it must either be caught like a try catch statement or it must be declared.
So basically, Java is saying you're doing something dangerous.
You're you haven't made it clear to Java that we are aware of the risks.
And so we need to do that.
So let me do it this way.
First, I'm going to say, OK, Java, we're aware we're aware of the risks.
And this program throws a file not found exception.
This program is going to crash and this line is never going to actually print.
So now that we've said that our program might throw this, then Java will allow us to compile and run this program.
And because I gave it a bad file name, Java is going to freak out and this program will crash.
So this is what it looks like when the program crashes.
And to answer your question, Anil, like this is this is not a good user experience.
This just absolutely crashes and it's just nasty.
So instead of just saying that we're aware that this might happen, Java allows us to use the try catch block where we can say, OK, if that actually happens,
we can catch specifically this file not found exception.
And now we can just be a little bit more a little bit more kind to the user so we can have a print statement that says something like.
Well, you know, we're just going to print anything out.
Oh, come on.
Yeah, I definitely know what it feels like to fight until Jay.
Sorry.
Oh, my gosh. What is going on? My caps lock is stuck somehow.
It's probably. I'm going to refresh my desktop.
My keyboard is all strange right now.
OK, there we go. My shift key was stuck. Sorry.
That file doesn't exist.
And now we can like refer to the file name here.
File name.
It looks like file name is showing up in red because it's out of scope.
We haven't talked about scope in Java, but this variable file name is declared here inside these curly braces.
The way that Java scopes things is with the curly braces.
So I have to actually move it out here.
So now that file name is declared outside of these curly braces, it's now available in this context.
Let me run this one.
And what we're going to see is we get a much nicer.
We have our own custom error message.
So that's what the catch allows us to recover from errors and behave better.
Let's see.
So I want to show you how to interact with this file in general.
Once we have a scanner, the general pattern is we can create a while loop and we can say, wow, the scanner has a next line.
Just one question. Yeah.
So we had a file not found except now.
Are there other exceptions?
Yeah, if we actually look at the docs here, Java docs scanner, we're going to look at Java docs more and more.
This is probably our first time looking at it.
This has a few examples of how to use a scanner.
If we look at this is the make my text a little bit bigger.
The documentation shows that this is kind of the method signature for this.
So we're saying we're creating a scanner.
So we're saying we're creating a scanner.
The scanner has the file parameter and it says that it throws this specific exception.
So this method will only throw this exception.
But there are other exceptions that exist.
Maybe.
Maybe it would be like there's an encoding problem.
So like maybe the file exists, but something about the format of the file is wrong.
So, yes, there's definitely other exceptions, but this is the only one that we'll have to deal with right now.
And.
Looking through every window until I get somewhere.
All right.
Here is a.
Let's just see if we can get the program to print out lines right now.
System out.
Print when here's a line.
Let's see what this does.
I haven't updated the file name yet.
And I'm going to do one more thing.
Scanner dot close.
I think that's just a polite thing to do.
What does it know where to pick up the file?
In this case, I made I went out of my way to make sure this this file was directly next to the program.
Except it looks like it still has trouble finding it.
So that's frustrating.
Let's see if I can.
Let's let's see if this works.
Have I spelled it wrong?
Huh.
What about if I drag this in and try and refer to it?
Oh, boy, I didn't like that.
OK, just open it.
That's not the worst thing in the world.
Titanic dot CSV.
I think that would work.
Maybe we can be a little bit more explicit.
Is it the other backslash?
Maybe.
Well, I moved it.
I moved it here.
Let's see if it actually maybe.
Yeah, maybe it is still in downloads.
Let's see.
Now, what's right here?
Huh.
Can you specify an absolute part?
Yeah, that's what I'm trying to do right now.
So I guess it's users.
I think my username here is Jaluso desktop Java and then Titanic scanner and source.
I think I'm going to find it.
Yeah, I did a dot slash on it.
I had trouble with that.
I think I think this is.
Boy, it's frustrating.
Yeah, I tried the dot slash.
I tried that.
Well, what if we did something really.
Lazy and put it at the top of our.
See directory for now.
That's any try adding a.
Error print line for get absolute path.
See where it's.
Yeah, that's a good idea.
I do. I do like that idea a lot.
Let me try this nasty hack to.
There we go.
That's that's a bit ridiculous.
Yeah, let's let's let's do your job.
A print print path.
What's someone saying system get property user dir OK.
Try it out.
System out print plan.
OK, let's try this.
Ah, interesting.
You know what? I think I see what was going on.
Good, good, Colin Neal.
OK, let's try this.
And that that totally works.
OK, I think this is a good moment for a quick break.
When we come back, let's come back at 130 and I want to get you all set up with this file and we're reading this file and then.
The sort of exercises that I have set up for us is getting this set up so we can read the file.
And then these are questions that I thought were interesting.
We're trying to figure out how many passengers there were, how many male and female passengers there were.
That's just easy data.
Some interesting stuff about how much they made off the tickets, maybe what the average price was.
The intention there is to give you some practice dealing with integers versus doubles.
And also dealing with we're going to read these files in as strings and you're going to need to convert a string to an integer.
So we're going to have to look up Java tools for that.
And then, oh, maybe some more morbid stats about like how many people survived.
So let's take a little break and we'll come back at 130.
Does that sound good?
Steve, just a quick question. Yes.
If I go to connect on my desktop, it shows me a blank screen.
Yeah.
Here, let me use my other computer to see what's going on there.
All right. Let me see what's going on. Yeah, I do see that.
OK, that's odd.
Hmm. Is it just me?
No, I have the same. I have the same problem as you.
Let's see what we can do.
We need to talk to.
I bet we can fix this with basically restarting your machine.
The fourth stop.
Yeah.
You're seeing that in like the configure button.
In the in the main, you know, yeah, yeah, it's just a stop.
It's a four stop.
Restore. Yeah, I would try one of those.
Maybe stop is sufficient. I don't think four stop is going to do anything too bad.
I want to try for stuff. OK.
Oh, it says it is very likely to damage the device.
Try stopping machine from it.
Yeah, I guess I should.
If we could, we would.
I did stop.
Let me see. Is it me or just say I guess I'm just curious.
I cannot get.
Make training.
Oh, yeah.
See who else is around.
I'm here. Hello.
I'm here. Excellent.
Very quickly. Are you are you clear? You created a new folder.
You did it.
Yep.
Let's see.
So I went I created an entire new project.
I'll let you follow along.
So I've got.
Yep. I'm staying right here.
How did he get to the new?
Yeah, it's kind of weird.
Let me let me follow along on your screen as well so I can see what's going on.
Let's see.
Yeah, you have to click. OK.
Very first thing is you click on.
I guess it's called a hamburger menu, but it's these like it's those lines right there.
Yep. Why they made that is beyond me and then say new project file new project.
No.
Roger. Yep, exactly.
And we're going to call it.
Yep.
Because that would a capital T right capital T. Yeah, I called mine Titanic scanner.
I don't. You should capitalize the S there.
Yeah.
And this is Java.
Great.
Yes repository.
You can leave everything default.
Press the create button towards the bottom.
It's going to ask you if you want to open another window.
Choose this window.
Checking. Don't ask again is fine.
There you go.
Excellent. Now we have a fresh.
Project.
OK, I downloaded your.
That CSV and how did we get it in here again?
Yeah.
Put your cursor down towards the start menu and click the folder icon.
Oh, OK. Yep.
And now we've got to go into downloads here.
Downloads.
That's where I put it.
Yeah.
Which yes, which.
Desktop is it looking at?
Because it's it when we download it, we download it to our personal desktop.
Right.
I think you can drag it in, right?
Yeah, you can drag it in.
Maybe it's saving.
Dragging it in into our desktop.
I guess I don't know where it where it downloaded to.
So you're going to find out where it downloaded to.
We can download it again.
I downloaded to my desktop.
OK, I downloaded to my desktop as well.
All right, Neil, let's click on desktop instead of downloads.
OK, I downloaded to my downloads.
Yeah, I don't think it allows you to copy from your local desktop to.
Yeah, this is a virtual machine.
Yeah, but should be able to copy where it doesn't for some reason.
Does it?
I think I see the yeah, I did this within the virtual machine.
So, oh, you downloaded within the virtual.
Yes.
So you OK, let's let's start by opening here, Neil.
You could open Chrome inside the virtual machine.
Yeah, I see what you say.
Yeah, I was a bit confused by the which desktop, but I see the confusion now.
Oh, and now we got to go to Stephen dot com, spelled with a five.
Five T E P H.
Well, yeah, five T E and I'm a P H E N.
Yep.
Stephen, you got to use a P H instead of a V.
It looks like you have an N in the middle there as well.
OK, it downloaded it.
OK.
OK.
OK, how do we import it, Steve?
Wait one second. I want to get Neil set up first.
You can you can drag it.
That's correct.
Yeah, you drag it and I put it on, you know, you just want to figure out where to drag it to.
So if you feel like it's all like overlapping on these screens.
I guess you can copy it.
I don't I guess if you could you could probably make a folder and copy it there.
Right. I opened those fields and just drag from one and put it on top of the Titanic thing and then it seems to do.
How do I open these two screens?
Side by side?
I know.
Yeah.
Many buyers want, I guess.
Hey, Neil, can I take control of your computer to get you set up?
Excellent.
Come on in.
Confusing with.
Oops.
Oh, OK.
Yeah, we'll get you set up with the interesting stuff.
OK, let's see if it's in downloads now.
It is and I'm having trouble dragging this thing to.
Well, it's asking whether it's.
Yes, I said yes.
Well, I mean, there's no other option.
Yes, that's true.
That's kind of funny.
Yeah. OK, so here we go.
I feel like I'm close.
It's dealing with the windows that you have to kind of.
Yeah.
OK, I think I got it.
All right, Neil, I got you set up.
Right here.
So I guess I'm going to leave my computer up.
And yeah, let's get everyone set up with just a basic scanner.
And once we once we get that set up and we run the program and it prints out all the lines, we'll be able to do more interesting things.
I need help bringing that.
Yeah.
To. Yeah.
We see what's going on.
You mind if I take control, Loretta?
All right.
All right.
Here we go.
Take the wheel.
Yeah.
OK, let's see.
We got to go over and say Stephen dot com slash Java.
And I can make a little bookmark here.
And now I'm going to save this one.
Yeah, downloads is fine.
And now here's the weird part.
Yeah, we got to open up this folder.
We got to go to downloads.
And if I'm lucky, yours is way easier to manipulate.
Oh, there it is.
There you go.
And then I'm just going to delete this stuff because we're not really using any of that.
And all right, computers are yours.
OK, thank you.
You're welcome.
Let's see, Eric, I haven't checked in with you a little bit.
How are you doing?
I'm there.
I didn't see him there.
How about you, Coy?
How are you doing?
Oh, yeah.
Just a reminder that it's easy to mute yourself.
Yeah, exactly.
You can take a look.
OK, let me take a look.
Yep, you got it there.
Excellent.
So now you can start.
You can kind of gut the code that's already there and start writing some code that gets the file name and the file and the scanner set up.
OK.
Steve, you were checking on me.
I was muted also.
I'm off now.
Yeah.
Yeah, Coy, you're looking great.
It's probably complaining about the catch statement.
Looks like you're working on it.
Yeah, OK, great.
I'm getting a phone call from someone.
I think I'm going to take this.
That's great.
All right.
Thanks for the call.
Have a good day.
All right, I'm back.
Steve.
Hey, Neil.
Let me see what you're doing.
Take a look.
See, I'm kind of trying to figure out what data is.
Yeah, I'm looking.
Can you hover cursor over scanner with the red line?
Ah, in your catch statement, you've written file system not found.
So it looks like it probably imported something incorrectly.
So yeah, it looks like you accidentally imported file system not found exception, and you'd need the file not found exception.
If you hover if you hover over that scanner with the red underline again, click add catch clauses.
Yep, right there.
There we go.
So that you can delete your other catch that you have set up the one that's all red.
Yes.
And that is an example.
You're probably going to have that extra curly brace so you can delete the curly brace online.
Twenty one.
You have those three import statements.
Do we have to put that in there?
Yes, you definitely need those import statements.
And you know, you can delete line three as well.
I have to take a phone call, but I'm getting back into it again.
Yeah, no problem.
I didn't know what the hell I was talking about.
And what did you do with the path?
Let's see.
With what?
The path.
Yeah, I think you just need a I think you need a dot in front of slash source.
Yeah, you need to period dot.
I think that'll make it work.
Otherwise, it was a dot says this path.
Okay, that means I see your error, Neil.
Input mismatch exception.
What the heck?
Interests.
Okay, here's what's going on.
On line 15, you've asked for next int.
You should say next line.
So the exception that's happening there is you.
Yeah, you're asking for an integer, but it's say it's whatever it's reading in the file.
In fact, I just happen to have the file open and like the first thing in the file is something that says passenger ID.
So you're trying to convert like a string to an integer and it's saying that's a mismatch.
So instead of writing integer, I equals scanner next int change.
I have to say string line equals scanner dot next line.
Say that again.
Thank you.
Yeah.
So the problem is on line 15, you're having this.
You're telling the scanner to give you an integer.
Okay.
Yeah, here I'll highlight it on my on my screen.
I don't know if you can see my screen right now.
Okay.
Got it.
Yeah.
Lorena, I'm checking out your screen.
Yep, exactly that.
Just make sure you get those semicolons.
Yeah, got rid of everything.
And that looks pretty good.
Yeah.
Okay.
I had that.
But then when I heard me all saying, do we need to put the imports?
I tried going up a line to put those imports in there and it kind of got rid of everything else.
Right.
So how do I do those imports?
You should be able to just to type like text.
Maybe you can use the arrow keys to move your cursor around.
That might be maybe what you should be able to click there.
That looks good to me.
You can press enter twice to make some new lines.
And now you can go up there and do some imports.
Yeah.
I'm like darn it.
Delete everything.
Okay.
Let me let me give you one tip.
Okay.
With your try, you've done parentheses and you need to you actually need a curly braces.
Okay.
And I haven't yet looked at why all of those things are red.
I was going to go back and put those imports.
I want to I want to show you a way that the red highlights are going to do the imports automatically for you.
Okay.
But you got to fix the try parentheses first.
Yeah.
Replace that with a curly brace.
Nice.
And then let's do it on that one as well.
Yep.
Great.
Okay.
Yeah.
Now if you click on any of these red things, let's try a left click or maybe it's just sometimes you just kind of have to hover your mouse over it.
It's a little finicky.
There we go.
Okay.
If you now you can click import class.
Yep.
And now it just totally writes that import for you.
Okay.
Yep.
There we go.
Now put your mouse over the scanner on the right hand side.
It's got the red underline under it.
So whenever it sees these errors, it often suggests things you can do to fix it.
So if you click add catch clause, click on that and boom, it just completely creates that for you.
Awesome.
Yeah.
IntelliJ is not always bad.
I like it.
Okay.
Thank you.
You're welcome.
Yeah, I'm looking at yours right now.
So you've got the you've got the line.
You're printing out all the lines.
So yeah, the next step for you is we want to split the line into basically columns.
And the way that we can do that is you can say why you can say between your line and where you print it out.
You can say line dot split.
Yeah.
So the idea is that we get the line.
Yeah, there you go.
And when you say split, put a comma.
You're going to have to you're going to need to do parentheses first.
And then inside those parentheses, we're passing a parameter and you do double quotes with a single comma in between two double quotes and make sure you use double quotes.
Nice.
Do me one favor.
Can you highlight that everything you just wrote inside that print statement?
I want to take that out of the print statement.
So let's get all of line dot split and the parentheses.
Basically, I want you to make a new line between lines 12 and 13.
Yeah, cut that text out.
And you're going to need to.
I don't want you to split inside the print statement.
I want you to create a new variable.
Here, I'm going to write it on my screen so you can see what I'm talking about.
Okay.
Yes.
Okay, let me do a lap and see how everyone's doing.
I have the original mistake.
I think if I break it from my wrong spot.
Yeah, I'm looking at your screen, Neil.
That's an easy fix.
You can delete that bracket and move it further down.
So I would put it after scanner dot close.
Oh, actually, no, you don't even need a you don't even need a bracket because that bracket for the try is actually in front of the word catch online 20.
So your brackets are okay.
I see.
Yeah, I see.
Oh, it's got to be capitalized.
No, hold up.
Hold up.
Compare your line 13.
Okay, online 13.
You said new scanner equals new scanner.
If you can see my screen, your line your line 13 is equivalent to my line 10.
What you need to do online, put your cursor online 13.
You need to get rid of the word new on the left hand side.
So see how like above that you said file file equals new file, you kind of need to do that same pattern over here.
You need to say scanner scanner equals new scanner.
And I think I think you pressed the insert button on your keyboard just because I see that your cursor is very blocky.
I think you need to press the insert button on your keyboard.
There you go.
Yeah, I think you need to click between the word scanner and equal.
And now you can write scanner with a lowercase s.
Oh, okay.
Yeah.
That's great.
There's I see two more syntax errors.
You need a semicolon at the end of line 10.
And on line 21, you need to delete a comma.
Great.
That is a good looking program.
Run.
I think I think it will run.
Let's see.
Yeah, let me let me see.
Let me think of what's going on here.
I generally like it.
Okay.
Put your cursor online 22.
You're missing one curly brace right there.
Oh, yes, definitely a closing one.
That looks that looks better.
I missed I missed that.
There you go.
That's great.
All right, I think everyone's got this.
Let me just do a lap.
Eric, I'm taking a look at yours.
I think on 14, if you change the word name to line, I think you can probably run that program.
You'll be looking pretty good.
Yeah, go ahead and run that.
Let's see if it works.
Andrea, I'm going to take a peek at what's going on on your side.
Andrea, how you feel good about what you're doing.
Except for this line 22.
What?
How do I do this equation?
Gender equals.
Yeah, I like everything you have right there.
I think that's perfect.
It's getting a problem here.
Okay, on 32, you have a parentheses.
You have a parentheses.
Yeah, yeah, yeah.
Yeah.
Yeah.
Right.
Totally.
And then you got one at the end of your string.
You need to you need to delete the parentheses between the end of the screen.
Yes.
There you go.
Yeah, that's awesome.
I love I love what you've done there.
And Neil, I see what you're doing.
You've got you got the theme.
You got a you've got the gist of it right there.
Looks like you're just printing out the line right now.
So you can probably you know, I love your variables.
That looks great.
One note about the variables is, oh, just to be more stylistically Java, you should probably have those be like, oh,
you should probably have those be lowercase variable names.
You know, I'm not going to murder you, but someone else might.
I took it from the header over here.
Yeah, I see the inspiration.
I guess I had a question.
How did it know not to pick up the header?
You know what?
That's a really good question.
I I'm not convinced it did.
It totally did print the header right there, actually.
Or maybe it is because I guess it to see what it did.
Yeah. Yeah.
I mean, you're printing out the line as it is.
But yeah, you are you are mentioning a good a good point.
We are going to have to make our program account for that at some point.
OK, we see how we can print.
OK. What do we got?
Lorena, I'm looking at yours.
It looks like you're able to print the file.
That's great. Yeah.
Now I need to go go to the individual field.
Yes.
You kind of get how we we haven't really even talked about this formally,
but do you get the idea of using split to access the fields?
To split the line? Yes.
The fields.
Yeah, kind of.
But is it is there a way to just get the field itself or?
No, we have to.
There certainly are like CSV readers in Java that we could like download and install, but we're doing this very manually right now.
So let me see. I need to find an example.
Here's a just in case anyone else is is is at this point, this data set is kind of annoying when it lists people's names.
Their names have commas in them.
So they listed last name and then the first name.
And that kind of screws up the the order of how we're accessing the columns.
So watch out for that.
Let's see, Anil, I'm looking at what you're doing.
Looks like you're dealing with some scope problems.
You're you're trying to interact like you have your your variable Titanic.
I think you wanted to interact with it more more on lines like what's that?
I want to just print the array, see what it has.
Ah, gotcha.
Like, OK, yeah, is it nice unless until I think I had to.
Oh, I think maybe this has to be outside, I guess.
Right. I like what you have written there.
So you you're reading the file, you're you get the line, you're splitting the line.
I think this outside. You're right. It's outside the scope.
So the declaration has to be earlier.
Well, what are you trying to do right now?
I just I now that I have the array, I want to print it, then I can manipulate it and all that stuff.
So yeah, the declaration of the array is happening here.
Yeah. So you see, it doesn't like that.
I like and I think, OK, you've got your array declared.
You've got you've got all your you're getting all the properties out of it.
So if you wanted to see a specific property, you've got your your print online.
Thirty one instead of printing out the entire line.
You could you could print out any one of the properties that you've just plucked off.
Right. But I want to deal with the whole at one time.
Right now, I can read it one line at a time.
Ah, so I want to just come out of the while loop and then say, show me what I have.
You have to print it off one at a time.
But what if I move this guy outside because it's a declaration, which is
it's this declaration, which is I think trying it out because it's within this scope.
So if I move it outside of that and I put it somewhere here.
Well, now now you're doing it before it's even read it from the file.
Well, sure, you can declare it.
But so let's say I get it here.
So let's say I do.
I say, yeah.
So good. Um, then I don't declare it here.
I just assign it here.
Yeah, I get I get that.
It's still going to do it line by line.
Here, let's be let's.
Uh, are you trying to like print off people's names or at this point?
I'm just trying to say, just show me what's in the area.
So I'm yes, I was like over here.
Yes. I collected the built the array.
Just show me the full array at one time.
That's good. I mean, you're already getting that by printing it offline by line.
I think.
So I guess he is.
So I have the area.
I'm done with all that stuff.
I'm done with the file and I now want to just work on the area.
So I'm here.
I'm outside the whole thing.
No, that's here's.
If you want to do something like counting how many like passengers there are, let's let's let's let's assume we do that or or how many you know the people sex male or female.
The way that we're going to do that right now is up where you've done your like maybe on line 10 or like outside the wild loop instead of trying to like capture the entire array.
Let's create variables that just count stuff.
So, yeah, instead of trying to interact with the entire rate exactly like that.
Let's just say exactly.
I love it. Do that.
And I guess I have to do this.
Yes, that's more stylistic.
I appreciate it.
Okay, let's do this.
Yep.
And you should you should initialize those to zero.
That would be proper.
Yep.
Great.
You think you're saying this or the formula over here.
You can increment them inside the wild loop.
And then after the wild loop, you can print them out.
Right.
I guess what I was trying to do is I want to just work on this array and then count it in the array.
I don't have a good way to explain it.
But don't try and do that.
Yeah.
Go ahead and delete that line.
We'll do some stuff after after we do like some line by line manipulations that might be more in line with what you're trying to do.
Are you suggesting I do it here?
Yeah, in there, you should increment those different values.
And then.
Okay. Yeah.
And then after the loop, you can print those values out and see what they are.
Okay. So that's just an if that loop.
Yes, absolutely.
Okay.
Okay.
What does that not like about my R E G E X colon?
Delete that.
Leave it.
So somehow that text got in there.
Just delete it.
That's not something that we actually write.
So it's perfect.
Just like it is right there.
So that text was typed in, but it's not supposed to be.
Although your screen still shows the word rejects with the colon.
That's just more of like a hint.
It's trying to tell you what that string represents.
So the way you have it there is good.
I like what you're doing.
You have the line.
You're splitting the line.
Here's something you can do right now.
Change your print statement so that instead of printing out the line, you can print out.
You can access that array called cells and you could say cells at zero or cells at one or cells at two.
If you change your print statement on line 18 and you print out cells at zero, you'll see that you're actually accessing things.
Specific properties on on a line.
Yeah, I'm trying to.
Yeah, I see what you're doing.
You've got the right idea.
Boy, I'm going to see what's going on over in your world.
Looks like you're kind of dealing with this whole name problem.
Yeah, I have one idea.
Make two fields for the name.
Pluck off the last name first and then the first name.
This is confusing and it's kind of messy data.
I probably could have manipulated this data.
It's annoying that the commas are there.
So when I'm trying to print the cell, it's all just really, really easy.
Is there a way we can almost a different way?
It's not worth it.
You should make two variables, one for the first name, one for the second name.
But it's still the actual myth that you can see.
Right.
I don't try that.
I can't.
I don't totally understand what you're saying.
You're saying something about parentheses in the name.
Yeah, I mean, that's OK.
It's messy data that we're going to choose to ignore it because it's yeah.
Yeah, I apologize for the messy data.
Yeah, yeah. So I'm trying to like, what is this?
OK, I'll do it.
OK. Yeah.
Yeah, we could do any we could do some fancy stuff.
But really, I'm just sad that I had such messy data in the first place.
And after all, garbage in garbage out garbage in garbage out classic Gigo.
Then you put the DWIM command.
What's DWIM?
Do what I mean.
Do what I mean.
That's a good one.
I like that.
Do what I mean, not what I say, huh?
Are you also import the drift command?
Do the right thing.
Ah, do the right thing.
Detroit.
Yeah, Koi, that's that's wonderful programming.
I like what you're doing.
Neil, watch out on lines 18 and 19.
Looks like you've used some you've mixed some curly braces with some straight brackets for your array.
There you go.
Good fix.
I have a message catch without dry.
Yeah, let me see what's going on, Lorena.
Interesting.
Ah, you are missing a closing curly brace for your wow loop.
So you need to press enter and then do a closing curly brace.
There you go.
Oh, yeah.
And that line shows me now that yes, it's closing it.
Yeah, because they didn't have it before.
Yeah, exactly.
Yeah, exactly.
There's the line.
Yeah, well, there is a line, but it's sitting somewhere else.
Yep, that's why that's why I make such a big deal about indentation.
What this is going to do.
So here's something.
The data is messy.
Go to line 19 and change where you're trying to get their gender change up from a four to a five.
It's just there's some there's a comma in people's names.
So it it it funks up our number system.
Great.
OK, OK.
So now.
Next.
Yep.
And the idea to count people is before the while loop, you can make two integer variables and then you can increment them depending on the value of gender.
Hey, yes.
So I'm wondering why it gives me a zero.
It's I guess it's not sex.
Yes, is it a common thing?
Try using dot equals instead of equal equal.
Here, I'll.
I'm going to write taking a two minute break.
Excellent.
Yeah, for sure.
That sounds good.
But the question is, is it in the right field?
Yeah, that's a that's a good.
That's a good question.
So this is what you're saying, right?
Yeah.
So definitely like you definitely need the dot equals.
I think there is an error with the numbering.
Oh, you're missing another parentheses there.
Go up, go up to line 20.
Well, here's here's a yeah, yeah, yeah.
Go ahead and print stuff out for sure.
If you print out the value of sex, it will go to.
Yeah.
And make sure you do that before the if statement.
OK, yeah, yeah, you're right.
I remind why is it putting an asterisk in front of the where that in front of the name?
Um, I'm not sure what you're talking about.
Everything looks pretty good to me.
Look at my output.
Oh, yeah.
It's because we have messy data that people's this file.
People's names have commas in them and it's screwing everything up.
So could we could we say trim or something?
Let's just ignore people's names.
It's it's it's truly the best way forward.
How about instead of saying change name to gender and change the number three to five?
Yeah, this this will get you moving forward.
And gender capitalized.
According to the styles of Java variables, it should not be capitalized.
And then you have to change that online.
Twenty one as well so that you're passing out.
You're printing out the correct gender.
No, it's going to be five.
This debt.
This messy is data that this data is messy.
The fact that the data has commas in people's names screws up the way that the numbers align.
Oh, I've never dealt with that.
And in coming data, we get the stuff all the time.
Yeah.
Yeah.
Next time I teach this, I'll have to fix up this file so it's not like that.
There you go. I like that.
I would just delete line 21.
Just because we don't have people's names right now.
And if you run that, I think you'll be looking pretty good.
Make sure you get a semicolon. Yep.
Yep. Doesn't matter what programming language we're in.
Garbage in garbage out.
But that's looking good. I like that.
What happened to the passenger ID?
Looks like we deleted the line that printed that out.
So you can.
Nevermind.
All right, Andrea, just checking in with you.
How are things in your world?
It's good. I was just playing around.
Yeah. Hey, look at that. That's awesome.
But if there were commas,
you see my count here should have been different.
I'm looking for female within life.
Sure.
So that should have been different from gender because gender would have moved,
would have contained something else if there was a comma.
Well, let's see.
01234.
You've got gender as as a cell five.
I think what you're screwing up is yes,
but it actually is five because the fact that the names have a comma in them adds one.
So I think you were making a mistake.
You were counting from one when you should have counted from zero for zero based indexing.
So, yeah, yeah, I like what you're doing. Go for it.
I think I see we're doing you're totally correct.
Oh, but what you're saying is all the names have a comma.
Yes.
I haven't looked. Yeah.
Both are going to be equal anyway.
Run your program. It's going to be.
I think you're going to see the discrepancy you expect.
Let me I got to remember this index.
Yeah, for sure.
To read.
Yeah.
Let's see. I'm looking at what you're doing, Anil.
Yeah, I think I did this and it says no.
Yeah, I don't know.
So I'm like, maybe I need to allocate it memory.
Like, OK, right.
Just let's get let's get you back on track.
Completely delete line 11.
OK.
And then if you can cut line 10, basically cut it and put it online.
So you can put it on line 21.
Or or online 21.
You just need to put the type before Titanic.
So online 21, then it gives us computer.
I know, but we're going to fix that once we get to it.
OK, you need to delete that line.
This one. Yes.
I know that you're trying to print the array, but it's just it's not on the table.
So that's an option right now.
That I think should work.
So, but I can I should be able to do this on the right.
I figured out that the sex is an age or whatever.
Yes, that that that's just because the count gets messed up.
So that's fine.
Yeah, does that run.
I think I think that's going to be good.
It's just that I guess.
I'm kind of like, OK, how do I.
It does. Yeah.
I didn't check the actual value, but that that looks correct.
So you're saying I should do.
So you say there's no way I can print it, I guess, from the area.
I guess I could I could do a while loop or the array itself.
Right. Well, should be the there's only OK, the file exists.
Right. And we're reading it line by line.
Right. And then the only place that an array exists here is we take the line and we chop it up into its individual components.
So so really, there is no big, large array that exists.
There's just not.
There's the file and we're reading it in line by line.
Now, like, yeah, it's just a single line thing.
It's because we didn't allocate a memory to it.
Oh, it's only a single line at a time.
Yeah. So, yes, yes.
Definitely like when we talk about some of this like Java collections frameworks, we have ways that we could build up an array like that.
But we haven't completely learned that yet.
Steve question. Yes.
How do you end a job that's run away.
Oh, no, it's fine.
I did a control to see and then it said stop and rerun.
But that means you have to have a fix.
And it was fine because I had a fix.
But if you don't have a fix.
Yeah, there's a in the bottom left by your output.
There's like a little trash can icon.
And I think that kills it.
You're very close.
Oh, wait, that's that's actually just a clear that just clears the output.
Maybe like clicking the X by the main.
I don't know. Sounds like yours.
I mean, sounds like you're saying you have something like a wow loop that never ends.
Right. And it happened because I.
Yeah. But by the time I fixed it.
Let me let me make a little there is something because I know in studio there is so there should be.
Yeah, let me let me just whip up a program that does that on my end.
Great. I'm going to take myself just a little bit of break.
I'll be back in just a few minutes.
If anyone else wants to take a break, go as well.
I'll put up a little thing that says back at two fifty.
So feel free to keep playing around.
And yeah, I think I think we'll get a chance to kind of show off our different programs.
Let's see. Where is my editor at?
Here it is.
All right. I'll be back in a minute.
Seven.
Hey, Eric, are you around?
Hey, Steve, sorry, I'm here.
There's a lot of stuff going on in the house.
So, yeah, you're fine.
I'm just noticing you got rejects.
I haven't done any. I haven't done much.
Did I reject?
You don't need to write that when you see it on my screen.
It's it's not text I've actually typed.
It's intelligent trying to provide a helpful context.
And I can I can ignore that.
Do the strings and yes.
Yes.
OK.
OK, I'll get back into that.
OK. Yeah, just delete rejects in that colon and you'll be you'll be good there.
OK.
And I think we can kind of bring it back in and have a little review about what we've just done soon.
I told people I'd be away till two fifty though.
So who knows if anyone's out walking a dog or something.
I'm back.
Yeah.
All those dogs are driving me nuts.
What kind of dogs you got?
So right now I have three of them because I have one that belongs to my daughter who moved to Hawaii.
So it takes a long time to get all the people ready to ship them there.
So right now I have a German shepherd, a pit bull, Mastiff, girl.
And then the one that belongs to my daughter, the Norwegian elk hound.
Norwegian elk hound.
Yeah.
And they're supposed to run after the moose there in Norway.
OK.
All right. I'm looking at it.
Looks basically like a husky.
Kind of a little bit.
Yeah.
And the closest thing he's ever seen that resembles a moose, which I have on my property is a horse.
Yeah.
That's funny.
Norwegian elk hounds can be very hostile against other dogs sometimes.
I think he's really good with the girls.
Yeah.
I mean, you got that big girl there.
She probably keeps them in line.
Yeah.
Yeah, the Mastiff.
Yeah.
She got a big bull Mastiff.
Yep.
That sounds like a cute dog.
What's yours? I saw him in the back.
Yeah, he's a red Labrador.
Hey buddy. Wake up. Wake up. Come here. Come play.
Yeah, I see him.
Whoa, he's a big boy.
Uh oh.
Yeah.
Yeah, he's pretty good.
He's beautiful.
Thank you.
Yeah.
People don't even believe he's a Labrador often because people don't see red Labradors.
No.
But he's good. He's a good dog.
Well, people have never heard of my dog.
Yeah, what do you got?
I have a big one.
Swiss.
Oh yeah, you're gonna have to say that again.
The Berger Blanc Swiss. I can tell you with a Swiss accent because I was born there.
Yeah.
It's basically a German Shepherd, but it's not German. It's Swiss.
Swiss?
Swiss.
Swiss White Shepherd.
Yeah.
Okay, I see it.
Yeah.
Looks kind of like that.
Well related because Switzerland and Germany and all those countries touch each other, so it just looks like a German Shepherd.
Yeah.
But his hips don't go down. It's straight on top and it's got an extra coat of fur on it.
And they were bred to protect her as she's in the Swiss Alps.
They would take three dogs from the same litter and they would put them in the middle of her as she's.
And they look like she's put her land out. They're big and white and furry. They're platinum blond.
I'm looking at photos of them.
Their rear hip doesn't go down. Their spine stays straight all the way in the tail.
And they have these huge furry tails that they curl around and sleep with when it's cold.
Right.
They love the snow. My dog came from Russia. So the colder the better. It eats ice cubes all the time.
It just eats ice cubes? That's kind of funny.
Yeah.
All right. Let's bring it back in.
We'll have to do some more dog programs in the future.
I'll get some dog data so we can look at dog breeds or something like that.
Let's see. Yeah. So this thing. Let me.
I'm going to kind of fancy my program up over here.
Yeah. Next time I do this, I'm definitely going to have to fancy up the way we deal with these names.
But here's here's the main idea. I think everyone did a great job figuring out how to read the file in.
And the main idea of this program here is to chop it up according to the commas and start plucking values off.
And I didn't really give very good instructions about how to tally things up.
But the main idea that I was imagining is we make a integer for males and we make a integer for females.
And we start both of those at zero.
And then inside of here, we can say if gender equals male, we can increment that.
Otherwise, we can increment the number of females.
And now at the end of the wow loop.
We can say what we want to do. We want to say system dot out dot print when.
Here's the total number of males.
Here is the total number of females.
So let's try to run this program now and see how it works.
See what sort of mistakes I've made.
There we go. Looks like two hundred and sixty six guys.
One hundred and fifty three gals.
What else is interesting?
Do you want to see how many people survived?
Let's see. Is this real data?
Yeah, it's totally real.
It looks like I already had a variable for survived, so I need to say like total survived.
And now we can say, let's see, how about if survived dot equals.
One. We'll say total survived goes up and now system dot out dot print when.
Now I'm going to show you that this doesn't.
It's not going to come up to what we expect.
In fact, I'm pretty sure that total survived is going to come up as zero.
And there's a reason for that.
But let's see.
So let's see.
So let's see.
So let's see.
So let's see.
So let's see.
So let's see.
So let's see.
And there's a reason for that.
But let's run it first.
This runs.
OK, so just as I predicted, it says total survived is zero.
For the sake of time, you know, we're not going to let anyone guess.
But what's going on here is.
It's a type mismatch.
So we have the value.
We've got this string for survived.
And if we're saying we're seeing if that string, even though over here in the data, we know that the value is zero.
And the data, we know that that string is just zero or one.
Well, it's actually it's not the number one.
It's not the integer one.
It's the string one.
So when we're dealing with data, we got to watch out for things like that.
There is a difference between comparing it to an integer or comparing it as a string value.
I'm going to make a note of that.
Be careful to compare.
The value from the file to the.
String value of one.
Versus the integer value of one.
So I think if we run this again, we should get a more accurate survive count.
And there we go.
Total survived one hundred and fifty two.
That's suspiciously close to the number of females.
But I guess they didn't say the women and children first.
But we could get fancier and try and say, you know, what what percentage of men and women survived?
We'd have to make more variables for that, for sure.
Let's see, was there anything else interesting?
I thought it was going to be interesting to see.
I was kind of interested in the ticket prices.
Was there anything else?
Anyone any other interesting questions people have about this data that we want to try?
Well, we included the right.
And all right.
And it wouldn't it wouldn't have male and female.
Yeah, you're right.
You're right about that.
Ah, interesting.
OK, so, yeah, maybe you wouldn't ask that.
That's why the numbers.
Gotcha.
Good catch.
I didn't even notice that.
Interesting.
Yeah, because I got one fifty two.
That's why you got one fifty three.
Ah, good catch.
Yeah, I was even the way that I was actually thinking of doing that was I was thinking of making a boolean that said is first line equals.
True.
And then I was going to say, if this is the first line, then we could say line equals scanner next line and is first line equals false.
My plan was to do something like that.
I just read it before.
I just read it once.
Yeah.
And said line had kind of kind of like you just you just went like this.
Yeah, that that makes a lot of sense, too.
I like that.
But you have to be careful where you place it because I had to go through.
Right.
You have to do it after the scanner.
Next.
Yeah.
Yeah, you have to do it.
You have to do it after the try it.
Yeah, within the try.
Yeah, that's for sure.
Uh, yeah.
OK, that's that's really good, too.
All right.
Well, we don't have too much time left today, but I.
Oh, I want to get through this slide deck.
A lot of what we're doing here is is still pretty simple.
I mean, we're we're reading a file in.
I like that we got experience reading a file.
I like that we're chopping these lines up with commas.
We did have this problem where people's names have a comma in it, which is pretty annoying.
So I apologize for that.
But yeah, sounds like you are all no strangers to messy data.
Let's see.
And Neil, you kept wanting to read in the whole file and kind of like look at a bigger array.
Let me show you some more tools that we have.
So I'm going back in the slides now.
I want to show you part of the Java.
They call this the collections.
So we've been doing a lot with arrays in Java, and we've said that arrays have a fixed size when they are created.
The fact that arrays have a fixed size is pretty rigid.
In fact, if you wanted to do something like read in this file and maybe save each wine to an array.
Well, we couldn't even do that.
Without reading the entire file to figure out how many lines we needed to keep track of, we kind of need to pre compute it.
So Java does have another structure called a list or an array list, and it lets us have things that are like arrays that are squishier.
And when I say squishier, I mean that these things can start with a list.
And then we can add more elements to them or we can remove elements.
So let's take a look at what these lists look like.
Here are some common methods on the list.
So we create things like this.
This is kind of like Java, where we kind of like we said, scanner, scanner is new scanner.
You know, we're saying we have the type of something is a list.
The variable name, I happen to name it list.
And then when we actually create it, we say it's an array list.
I'll talk more about that in a moment.
The discrepancy between list and array list.
And here's something we haven't seen yet is we have an additional type here.
So we've seen things like integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer,
so we've seen things like integer X equals 99 or string name equals Steve.
But here we kind of have two types.
We have a type of a list and then inside we call these diamonds or I don't know what else you want to call them.
We actually say what type the list holds.
And now here are some for saying this is a list, but it's not just a list.
It's specifically a list that contains strings.
Common methods on the list are we say we can add a string to the list.
When we add it, it actually sends us a Boolean value back.
I'm not sure if that would ever fail, but maybe it can.
We can remove a item from the list so we can remove a string.
When we remove it, the return type of that function is it'll give us that item back.
This thing is kind of like an array.
Whereas an array, we use the square brackets and we give it an integer.
This list is it's not a bracket anymore.
I mean, it's not it's not an array anymore.
It's an object.
So instead of using sort of more low level.
Basically, we can think of an array as a more primitive type than a list.
A list is an object and a class, and it has these methods on it.
So I'm just trying to make a distinction between calling a method called git for the list versus using an array that has the square bracket indexes.
We can set an item.
We can ask for the size of a list.
There's a sort of a convenience method called is empty.
And we have a method where we can see if something is inside of a list.
So contains.
So the basic idea is that, yeah, we can create lists in Java lists.
You can set you can consider them.
The upgrade is basically that they're squishier.
So although arrays are fixed size, these lists have a variable size.
I'm going to show you something now on the next slide that we'll talk more about later.
So let's let's look at this with kind of squinted eyes and not look too closely at it.
I'm going to flick between the two.
Let me flick through these right now.
What I want to draw attention to is the type between the list.
So here on this first slide, we said this is a list and the list contains a string.
Well, what Java calls is a list.
And the list contains a string.
Well, what Java calls objects that behave like this Java has something called generics.
And I'm just mentioning this in case you see in documentation.
We'll look at this more when we build our own custom objects as well.
But Java has this idea of something called generics, which is sort of like sort of like an algebra for types.
So in this first slide, we are really specific and we said this is a list and that list contains strings.
Well, sometimes the way that Java refers to these things is it'll it'll use like a single letter E.
And that E just represents a generic type.
And so if you ever like actually programming your own custom list, you can actually refer to whatever type something is with a weird letter E like this.
So I'm just letting you know this because it is a thing that exists.
But huge caveat right here.
Don't get caught up in the details of this.
I'm just telling you this exists.
And the only thing that we need to know right now is when we make things like a list in these little diamond brackets, we just have to say what type we're using.
We don't need to worry too much about generics right now.
Just a preview of what's to come.
OK, so here's what a using a list actually looks like.
We have to import some things.
So I'm going to import the list and import an array list.
I should mention that now.
There's a type list and there's a type array list.
There's actually.
Different implementations of list.
So in Java, we say that a list is a thing that you can access stuff at indexes.
You can get things out of index.
You can set things at an index.
And then the specific implementation of it is this is a list that's made out of arrays kind of down under the hood.
Another version that you might see is you could say we have a list, but it's actually a linked list.
Does anyone use linked lists in other programming languages?
No.
OK.
Yeah, linked lists are.
It's a way to make a list like structure.
But instead of using an array, you kind of have a chain of associations.
We can nerd out on that another time, but we don't need to worry about that right now.
So here's what these lists look like.
We create a list and the list will start empty and then we can add things to the list.
So, OK, let's add a string by eggs, do some laundry, watch TV, rake the yard.
And already this doesn't seem like a big deal.
But when we had arrays before again, arrays would have started with just a fixed size and we can't just add things like this.
Here's an example of how to get the size of a list so we could print out how many things are in it.
The way we interact with the list is very similar to an array.
We still can use a for loop and we use I.
We say I is less than zero instead of saying length.
We refer to a method called size.
And now we can get these things in the list one by one and print them out.
Not a big deal.
Let me just check in.
How do you feel about this code?
Does this look like it generally makes sense?
Sorry, maybe I missed this.
Is this just a single column array?
Yeah, that's that's a good way to think of it.
A single column array versus like a two dimensional array.
Yeah, that's fair.
Why would I use one versus the other?
Well, in Java, an array only has one fixed size.
So if I go over to here and I make.
I spelled that wrong.
It's kind of annoying.
Refactor rename file.
Public static void.
I need to give that the name main.
OK, so if we have a string array and we call it chores.
Right off the bat, we need to decide how big it's going to be.
We have to provide it a number.
So now we could say, OK, well, chores.
Let me just make this smaller for the sake of something.
We'll say the first chore is laundry.
Zero one, two, three, four.
Let me get these indexes.
OK, so let's say that we have laundry to do.
We have cook cooking to do.
We have dishes to do.
What else do we have?
We've got a dog, a day walk, a Boston bag.
What else might there be?
I don't know.
Maybe we want to paint.
Yeah.
And then obviously programming.
If we let's see.
What is it complaining about already?
Contents of array chores are written to, but never read.
Yeah, that's fine.
Let's think a little for loop.
Well, you have string five and you have the sixth choice is the sixth.
Oh, yeah.
That's yeah, that's definitely what I'm trying to show.
Yeah. So right here it says like the array index is out of bounds.
That's exactly what I'm trying to illustrate is arrays have a fixed size when they are created.
Well, if we make a list.
We do not have to provide a size and we can sit here all day and add things to this list.
So I'm going to say like, OK, let's go for a walk.
Let's go for a run.
Let's take a shower.
Let's dress.
Let's eat breakfast.
Go to work.
This the the entire difference between an array and the array list is is truly that.
This thing basically has infinite capacity and it will grow and shrink as we as we as we just call add or like remove things from it.
So that's the difference.
Let's see if we actually if we actually run this.
Yeah, we can't.
So it crashed because we tried to add something out of index.
So it says array index out of bounds exception.
Now, what is the advantage of the array?
Yeah, you're saying if if a realist is so much better, why would we ever have an array at all?
Yeah, that's a fair question.
The array is just a more fundamental.
It's a bit it's lower level in the computer.
Think of it instead of asking why the array is here if it's so bad.
We should instead be grateful that we have the array list because it's so good.
Basically, the the array is the more fundamental.
It's yes, it's lower level.
An array list is built out of an array.
Later on, we can we can nerd out and investigate how to how to build an array list out of an array.
But yeah, basically the Java the list is just such a common thing that Java included the list on top of the array.
Yeah, let's see.
What else we have?
Three seventeen. We only have a little bit of time left.
I'm going to skip this slide. We can talk about this later.
Here's one other tool.
I think you're going to be a little bit more impressed by this one.
This this data structure is called a map or a dictionary.
So whereas the array only stores things in a rigid straight line at indices, a map or a dictionary keeps track of key value pairs.
So we have to import it.
That's that's no big deal. We're no stranger to import it anymore.
And here's how we can create something like a map.
When we create a map, we use these diamond braces again and we say what the type of the key is and what the type of the value is.
And this allows us this is a pretty powerful data structure.
It's they say that the interface is called a map.
And then there's a specific version of that called a hash map.
And here we're saying we can keep track of, I guess, a character in integer.
Here's some methods that the map has. We can put a value with a key.
We can ask what the value is for a key.
We can remove a key value pair.
We can ask and see if a key is associated in here already, or if a value is in here already.
We can look at the size of something where we can see if it's empty.
Here's an example of what types of problems we can program with the map.
So here I'm making a map, and I'm calling it a tally.
And I'm taking something like a sentence of the quick brown fox jumped over the lazy dog looks like I made a typo.
We can iterate over the entire sentence and get each letter.
And now if we take each letter, we can ask the map this dictionary map thing I call the tally.
And we can take each letter and kind of count up how many times we've seen those letters.
So to begin with, I say if we if we haven't seen this letter yet, I'm going to create a new entry that has a zero.
And now we can see how many times we've seen this letter.
And that's the count. And we can increment the count.
And now we can put it back in the tally.
So basically, we're just counting how how many times each letter has appeared.
Let me execute this code.
I think I've got it.
I guess I'll just create it here again.
That's right here. So if I if I run this, here's the output of it.
And we see these curly braces and we can see that the map looks like OK, there is one a there was one B, there was one C, there were two D's, there were four E's.
And we looked at this sentence yesterday and we know that it's supposed to be a sentence that has every letter of the alphabet.
But yeah, maps are a big deal. Maps and dictionaries are a big deal.
They definitely let us solve more interesting problems.
Any questions about this map thing?
All right.
So now let's see what else we've got today.
We've got custom objects, custom objects, example.
This is the last thing that we'll kind of just briefly look at today.
Java definitely allows us to create our own objects.
We can make a new file and a new class.
Most of the files and classes that we've had so far are files that have a main method that just execute some piece of code.
Well, as our programs get more complex, we can have our own files which define the shape of our own data.
And maybe we can build methods that allow us to manipulate our data in ways that we would want to.
So in this example, I've created a class which represents a address.
So Java.
Well, maybe I don't think it does.
But yeah, Java just doesn't have, you know, the program language does not come with something that represents street addresses.
So if we want to keep track of something like street addresses in our own program, we can create our own class and we say, all right, well, here's an address.
And this is an object which has a few different properties.
It has a street and address has a city and address has a state.
There are special functions called constructors.
And if we build a constructor, which is a function with these different parameters of different strings, we can get information in, and we can save it inside this object.
And now we can actually create our own objects.
So here we're saying here's an address.
The variable is called Apple headquarters.
And we say that's a new address.
And we pass in these strings.
And now we can actually keep track of our own data.
Does the gist of that make sense?
Have you ever used anything like that in any other language?
Not really. Okay. Yeah.
Well, when people are talking about object oriented programming, this is kind of the big idea.
It's really all about creating like we would call this thing an object.
It's all about saying like, okay, this thing is an address and we know that the address has these properties.
And then you can add other other methods on them that manipulate it.
Like, here's here's another example of an object like, okay, this is a point.
And we know that a point has an X and a Y coordinate or that's just what I've chosen.
I always I always like to write X X and Y Y.
It's just about searching for things in text files.
If I ever do like a single X, I get annoyed because it has too many hits.
But here's an example of a class where, okay, we're keeping track of like two coordinates.
And this is an example of like a function, which is like a distance function.
So one thing that we're probably not used to seeing here is the keyword this.
So this is a special word in Java that lets you refer to just an instance of one point.
In this example, we've created two points.
Here's a point. I didn't give it any information.
When we don't give it any information, we call a special function, which is actually this function.
So we're saving this with zero zero.
But now we can actually we can write code that says like P one dot distance to another point.
So we'll look at more of this definitely in the future.
I'm just giving you a little taste of what's coming up.
Also, when we build our own classes, this is sort of a behind the scenes look at we saw the dot equals method on like a string earlier today.
When we build our own custom objects and our own custom classes, we define what equality means.
So in this case, I'm saying we have two point objects.
There's this one and there's another one.
And so we actually write code that digs into the object and gets to like the primitive values that the object is made up of.
And now we say, OK, well, you know what?
This point is equal to another point if these things match and if these things match.
So in the future, we'll see what it takes to build our own objects and build up methods like equality and other sort of manipulations.
I hope that's not too much for the end of the day, but we'll wrap up for now.
I definitely want to.
Yeah, we're doing polymorphic next time.
Absolutely.
Do you have an agenda for day three?
Let's see.
Here's we go to day one and you go to slides.
This is my initial schedule.
I think we did pretty well for day one day to I said we're going to do more object oriented in classes.
We kind of if you look at day three, I said we're going to do more Java collections, which is kind of what we just talked about.
I would say day three is going to be kind of more definitely more of this object oriented stuff, which we didn't get too much into.
I can update this website to to say day three and say what what object what what things we're going to what things we're going to cover.
But it's going to be a mix of of object oriented and Java collections.
Yeah, that's about it for today.
But thank you so much.
Yeah, good working with everyone today.
I will lots of information.
Yeah, some people are easier than others and some of the concepts are familiar to some people and not others.
But I guess that's what we're different mix of people here.
Yeah.
Yeah, at the end of the day, you're you're a team and someone's going to be better at SQL and someone's going to be better at web stuff and someone's going to be better at data structures.
And I mean, that's that's that we want.
We want that right.
Lorraine.
Yes.
When you get the recording of the recording from Maria.
But she she asked me actually I didn't get it.
She sent me an email asking me to sign a document and send it back to her.
I guess it's whatever.
Okay.
To share.
Even really look at it because it came while the class was on.
I will probably get it signed by tomorrow and send it back.
And then we'll get it.
Yeah, I think that's just something you sign that says you're not going to share that, you know, that would that would put us exactly that would put us out of business.
Yeah.
All right.
We got it.
Yeah.
I'll be there in disguise.
I'll show up and see what you're doing.
Thanks, Steve.
All right.
Thanks, everybody.
Have a nice weekend.
Have a good weekend.
I'll see you next week.
Take care.                

on 2023-11-08

Visit the Java/JavaScript/React JS training course recordings page