Grade 11 –  Reading in a text file into an array or into parallel arrays (array sets). Without a delimiter and with a delimiter.

Grade 12 – Reading in a text file into an array of objects

GRADE 11

1) Reading in a simple text file using the Scanner class. We create a simple text file with 20 names that we want to use to name various sports teams. This is what the text file could look like . . . no delimiter.

Mercury

Venus
Mars
Jupiter
Saturn
Uranus
Neptune
Astroid
Comet
Rhea
Callisto
Io
Europa
Ceres
Luna
Titan
Hyperion
Janus
Oberon
Ariel

We place the text file where Java can find it. In NetBeans we place it in the projects folder. In jGRASP we place it with our Java code. We name the file SixASideBeachText.txt.

Here is a simple one-class, one main method program that will read in the text into an array of String.

Click here

2) Now we add more details to each team i.e. the team’s colours and the amount of money they raised. The text file now looks like this below . . . NOTE: No spaces. Each line is a record and the three fields on each line are separated by a hash character #; we call the hash the delimiter. (the hash leads to fewer mistakes from the keyboard than the traditional comma especially when working with double).

Each line has three separate fields. This is a problem because an array can only hold data of the same type and we need String and double. A solution is to use three arrays – one String array for the name, one String array for the colour and one double array for the money. 

Mercury#red and green#1767.99
Venus#red#1565.34
Mars#green#2316.39
Jupiter#white#1452.12
Saturn#white and black#2198.19
Uranus#green stripes#1554.98
Neptune#yellow and orange#1567.76
Astroid#orange#1784.87
Comet#red and yellow#2776.56
Rhea#blue#1655.45
Callisto#blue stripes#1589.34
Io#light blue#2578.54
Europa#purple and orange#1686.66
Ceres#blue and black#2686.44
Luna#purple#1654.34
Titan#black and red#1876.98
Hyperion#green and dark red#2876.34
Janus#grey and red#1879.45
Oberon#black and yellow#1879.53
Ariel#black stripes#2876.34

Here is a simple one class, one main method program that can read in the text file and stores the values in three separate arrays. We are using the main method to read in the text file. 

Click here

3) Add code to your main method that will print out the three arrays . . .

Venus red 1565.34
Mars green 2316.39
Jupiter white 1452.12

etc

Add code to your main method that will total the amount of money collected by all the teams.

Click here

4) The same program as 3 above but using OOP principals. Create a new package. In the package create a UI class and a new manager class. Cut and paste your original code into the two newly created classes.

The UI class must only create a manager object and call the “displayAll” method.

The manager class must only have a constructor method and a displayAll method.

Here the constructor method will read in the text file.

Remember that you will need to declare global variables for your manager class to work.

Click here

5) With sorting.

Of the 20 names in the text file 7 are planets, 3 are celestial bodies (smaller than planets or moons) while the last 10 are moons. We want to use the planets for the adult teams, the moons for the teenage teams and the celestial bodies for the children’s teams. Therefore we are going to need 3 different counters i.e. one for each age group. We are also going to need three array sets – one for each age group in order to keep the age groups apart. 

Edit the constructor method in 4 to sort the names into adult arrays, teenager arrays and children arrays (each with their own counter) We will have to edit the text file. Here is what the new file will look like. The letter ‘p’ for planets, ‘c’ for celestial bodies and ‘m’ for moons.

The text file looks like this . . .

Mercury#p#red and green#1767.99
Venus#p#red#1565.34
Mars#p#green#2316.39
Jupiter#p#white#1452.12
Saturn#p#white and black#2198.19
Uranus#p#green stripes#1554.98
Neptune#p#yellow and orange#1567.76
Astroid#c#orange#1784.87
Comet#c#red and yellow#2776.56
Rhea#m#blue#1655.45
Callisto#m#blue stripes#1589.34
Io#m#light blue#2578.54
Europa#m#purple and orange#1686.66
Ceres#c#blue and black#2686.44
Luna#m#purple#1654.34
Titan#m#black and red#1876.98
Hyperion#m#green and dark red#2876.34
Janus#m#grey and red#1879.45
Oberon#m#black and yellow#1879.53
Ariel#m#black stripes#2876.34

The declaration should look like this. One array set for each age group, each with their own counter

// adults
String [] teamNameA = new String[20];
String [] teamColoursA = new String[20];
double [] teamFundsA = new double[20];
int sizeA = 0;

// teenagers
String [] teamNameT = new String[20];
String [] teamColoursT = new String[20];
double [] teamFundsT = new double[20];
int sizeT = 0;

// children
String [] teamNameC = new String[20];
String [] teamColoursC = new String[20];
double [] teamFundsC = new double[20];
int sizeC = 0;

Click here

GRADE 12

6) An array can only hold one field, all of the same datatype like questions 1 to 5 above.

0 1 2 3 4
Mercury Venus Mars Jupiter Saturn

If however, we create a class, we are able to have many variables –  and they do not have to be the same datatype. We can then create an array of objects created from the class.

Index Name Type Colours Funds
0 Mercury planet red green 1767.99
1 Venus planet red 1565.34
2 Mars planet green 2316.39
3 Jupiter planet white 1452.12

Reading into an array of team objects from a text file with delimiters (using the same text file as above)

Of the 20 names in the text file 7 are planets, 3 are celestial bodies (smaller than planets or moons) while the last 10 are moons. We want to use the planets for the adult teams, the moons for the teenage teams and the celestial bodies for the children’s teams. We want to create an array of teams from a Team Class

Team Class will have the following 4 fields – name, age, colours and funds.

Edit the constructor method to substitute “adults” in place of ‘p’, “teenagers” in place of ‘m’ and “children in place of ‘c’

This is what the beginning of the template class “Team” will look like ie the properties followed by the constructor method.

public class Team {

private String name = null;
private String age = null;
private String colours = null;
private double funds = 0.0;

public Team(String n, String a, String c, double f) {
name = n;
age = a;
colours = c;
funds = f;
} // end constructor

Reminder of what the text file looks like  . . . 

Mercury#p#red and green#1767.99
Astroid#c#orange#1784.87
Io#m#light blue#2578.54 etc etc

Click here