Arrays and File Reading/Writing
Mr Chuang may do a proper Array vs ArrayList lesson. This will come later.
https://docs.google.com/presentation/d/1Yd9ZEMmAzwpjdE1xAPzFxesEY8c7YeMSenLmyEmyJsI/edit?usp=sharing
Arrays in Java
Square brackets will let you know the variable is an array of the chosen types. However it is curly brackets to initialise them!
We won’t be using normal arrays in this course!
But they do come in handy...
Collections - ArrayLists, Stacks and Queues
When we want to have a list of variables, we can store them all into one variable instead. These things are generalized as collections in Java.
In the world of Computer Science, they are further generalised into data structures!
The more data structures you know, the more problems you can solve!
How to get them?
Copy this into the top.
This should give you everything!
(This isn’t a good way but it’ll be fine for Level 2)
ArrayList examples - adding
These are similar to the Python Arrays.
It takes a bit of work to create an array with items already in it!
ArrayList examples - removing
One removes the String with the value “cat”
One removes whichever String is at index 3 (4th item).
Iterating through them - for each loop
There are 2 other ways to do it! We will just focus on the for each loop as it works for all collections!
We can even have collections of objects!
The idea is the same, except they are objects instead of doubles/Strings etc!
Iterating through pet objects
File Reading and Writing
We need a File Object.
We create it, and then we put it inside the scanner.
The algorithm will then run until the scanner runs out of lines.
Make sure the file is in the project folder!
Why Try and Catch?
Failing to find the file WILL crash it. We always need to be prepared in case that happens. If it does, no worries, it will just run as normal.
More on Exception
https://www.javatpoint.com/exception-handling-in-java
In short, if we’re prepared to catch, the program will be prevented from crashing.
Parsing Files - Video Game Data
Sometimes you will get lines of data that we may not always want.
Parsing the data
Using .split(“,”) will split the String into an Array. (NOT an ArrayList).
We can then use proper indexing to access the data we want.
Reading files and process them is CALLED parsing!
Parsing - You have seen this before!
How to evaluate: 6 + 4 / 2 ?
How do calculators parse the string?
Parsing - Summary
Scan every line until there’s none left.
At each line, split the string into an array.
Obtain the information we want based on index positions.
Create the appropriate object and add it into an ArrayList.
BabyNames hints
Make a new class to store information (Similar to Video Game)
Read the file in, line by line, create the new object.
Store the object in a ArrayList.
Now everything is loaded, ask what the user wants to do.
while(true):
We have more time now!
Key things:
Finish BabyNames - Either get Mr Chuang to do it or finish it today.
Start GUIs next week. We’ll assess after the holidays.