π― Unit 4 Activities
Table of Contents
π» ACTIVITY PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository - Specify the repository name:
CS2-Unit-4-Activity-#Replace
#with the specific activity number. - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositoriessection of GitHub! π - Now on your repository, click and select the
Codespacestab - Click
Create Codespace on mainand wait for the environment to load, then youβre ready to code!
π΄ ACTIVITY #1: Pokemon Dataset
In this activity, youβll practice using arrays and working with text files with a dataset about Pokemon.
First, review the notes on the File, Scanner, and IOException classes: π4.6: Using Text Files
PART A: Create Array from the Data File
The following exercise reads in a data file about Pokemon and prints out the first 10 lines in the file. This file has the extension .csv which stands for Comma Separated Values. All spreadsheets can be saved as CSV text files, and spreadsheet software can easily open CSV files as spreadsheets.
- Add the
pokemon.csvfile (that I emailed to you) to your repository by dragging it into the File Explorer tab. - Include
throws IOExceptionin the main method HEADER like below:public static void main(String[] args) throws IOException { ... } - Set up
File,Scanner, andString[](array) objects at the beginning of the main methodβs BODY:File myFile = new File("pokemon.csv"); Scanner scan = new Scanner(myFile); String[] pokemonLines = new String[10]; - Complete the
whileloop to read in the first 10 lines of the pokemon file using theScannerclass, save each line into thepokemonLinesarray, and print it out. Here is the loop structure:
int i = 0;
// 1. Add in the loop conditions
while ( ) {
// 2. Read in the next line of the file
// 3. Assign the line to the ith element of the pokemonLines array
// 4. Print out the line
i++; // increment line counter
}
scan.close();
- Your loop condition should check if scan has another line of input (
scan.hasNext()) AND also check that the line counteriis less than 10.- To read in the next line of the file, use:
String line = scan.nextLine();- To assign the line to the array, use:
pokemonLines[i] = line;- Finally, print out the line before incrementing the line counter!
PART B: Display Data Attributes
If you take a look at the Pokemon CSV file, youβll notice that each line contains multiple data attributes separated by commas. These attributes include each Pokemonβs name, type, speed, etc. on each row. Typically, the first line of a CSV file serves as the header, indicating the names of these attributes.
// pokemonLines[0] contains the first line of the Pokemon CSV file:
Number, Pokemon, Type 1, Type 2, HP, Attack, Defense, Speed, PNG, Description
Splitting Strings
The Java String class provides a useful method called split(String delimeter) that allows us to split a string into an array of substrings based on a specified delimiter which is a character like a comma or a space that separates the units of data. This method returns a String array where each element in the array represents a field of data from the line.
Here is an example of how to use the split() method to split a line of data with commas separating the fields from the Pokemon csv file into identifiable chunks of data.
The first line of headers in the file indicates that the 0th element of the data array is the Pokemonβs number, element 1 is the name, etc. We only need to save the data that we want to use. In this case, we want to save the name, type1, and speed.
// Split the line of data into an array of Strings
String[] data = line.split(",");
// Identify the data
// data: Number,Name,Type1,Type2,HP,Attack,Defense,Speed,PNG,Description
String name = data[1];
String type1 = data[2];
...
String speed = data[7];
String imageFile = data[8];
If we want to do math or comparisons with the speed, we can convert it to an
intusing theInteger.parseIntmethod that will be described in the next lesson.
Your program currently reads in some of the data from the pokemon file into a String array of lines.
- Complete a
randomPokemon()method to print out a random pokemon name:/// Write a function that prints out a random Pokemon name public void randomPokemon(int length) { // 1. pick a random number from 1 to length (the 0th row is the headers) // 2. get the line at that random index from the array pokemonLines // 3. Use the split method to split the line into a String array data // 4. Print out the name using the correct index in the split data } - In the main, call your new method using:
randomPokemon(i);whereirepresents the length of the file (number of lines we read in during the loop) - Run the program multiple times to see different Pokemon names.
- Update your
randomPokemon()method to print out other data attributes, such asdescriptionorspeed.