Write the code for this project:
Daycare Statistics(no hashmap, don't switch and case)
For this project, you will be reading a text (.txt) file into your program,
putting the data into arrays, and then running some statistics with the
array algorithms we have learned this unit.
Text file contains the following information about the students who are at a daycare center:
 First and last name
 Gender
 Ages
 Hometown
 Days attending
 Phone number
The .txt file will contain an unknown amount of rows, but each row will have the information above, in order,
and separate by a comma. Here is an example:
Maevery Smith,2,F,Washington,4,123-444-5454
Cedar Johnson,5,M,Springfield,4,123-567-2323
Jaspin Williams,4,M,Washington,5,123-444-9393
Charleston Brown,3,M,Greenville,5,123-567-1100
Faelina Jones,4,F,Franklin,4,123-400-5678
Idalia Garcia,4,F,Springfield,3,123-333-1212
Brigham Miller,1,M,Washington,3,123-566-1123
Tovin Davis,2,M,Franklin,4,123-223-2223
Evabeth Rodriguez,3,F,Greenville,3,123-433-1121
Tessadora Martinez,5,F,Springfield,2,123-999-0909
Anaveah Hernandez,3,F,Washington,2,123-222-3030
Albion Lopez,3,M,Franklin,3,123-222-1000
Jessalie Wilson,2,F,Greenville,4,123-566-9009
Garylen Anderson,3,M,Greenville,4,123-333-6644
Sylvalie Thomas,4,F,Franklin,5,123-455-0022
Eastley Taylor,2,M,Franklin,5,123-567-8765
Ranger Moore,2,M,Washington,4,123-999-1122
Sophiel Jackson,2,F,Washington,3,123-111-4233
Elisobelle Lee,3,F,Greenville,3,123-400-8766
Wrenlow Nguyen,5,M,Springfield,2,123-988-5444
Step 1: Read a file and put into arrays
Reading .txt files into your Java program will not be on the AP exam, but it is a great skill to review. In order to
read in .txt files, you will also be introduced to the try/catch block, as well as exceptions.
First, use the following import statement:
import java.io.*
This will import the FileReader and the BufferedReader classes that you will need in your program.
Next, create an array of Strings. Each row of the text file will be read into a String at each index:
String[] rows = null;
The following variable will count how many rows are in the text file. We need to know this information before
we create our arrays, so we know how many students we have.
int numOfLines = 0;
Next we will get into our try/catch block. Our data file is called "data.txt". If you are using Replit, you have
to click "upload file" in the upper right corner of your IDE. You will then upload data.txt there. When
reading text files, we need to use a try/catch block in case our file is not found. This is where our
IOException comes in. An IOException is an error put out by your program when it cannot read the
input correctly (such as not being able to find you file, or something similar). The try block says to "try" all
the code located in that block and if at any time the program encounters an error, the catch block will "catch"
that error and run the code in that block.
try{
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String line = br.readLine();
//determine how many lines
while(line!=null){
numOfLines++;
line = br.readLine();
}
br.close(); Good code closes the BufferedReader
rows = new String[numOfLines];
//makes each row into an array
Now rows will get created with the
appropriate amount of storage space.
BufferedReader br2 = new BufferedReader(new FileReader("data.txt"));
line = br2.readLine();
int i = 0; //index
while(line!=null){
rows[i] = line;
line = br2.readLine();
i++;
}
br2.close();
}catch(IOException e){
}
System.out.println("An error has occurred");
This is your catch block which will hopefully never run if
your code is correct!
This lets us read a single line in our
text file and store it as a String
Keep reading lines to determine how
many lines in the .txt file there are.
Now that rows has the appropriate amount of space, we will repeat the
code we just went through and store each line of the txt file into the
array, where each index holds a single String with the row information
row[0] will be "Maevery Smith, 2, F, Washington, 4, 123-444-5454"
row[1] will be "Cedar Johnson, 5, M, Springfield, 4, 123-567-2323"
and so on…
Step 2: Make arrays of your data
Now you have all the information stored in a single array, with each index holding all of the information for
each of the children in daycare. You will now want to write the code to split up all the information so that you
have an array of student names, an array of ages, an array of genders, etc.
Be careful with your indices; the index is what keeps track of each child's information across the different
arrays. For example,
row[0] contains "Maevery Smith,2,F,Washington,4,123-444-5454"
After the split,
names[0] = "Maevery Smith"
ages[0] = 2
gender[0] = "F"
hometown[0] = "Washington"
days[0] = 4
number[0] = "123-444-5454"
Each row will have a comma to separate the data. If you taking a number that is represented as a String and
putting it into an array of ints, you will need to convert the String into an int. This is no longer tests on the AP
exam, but again, it is good to know. Here is an example of the parseInt method:
String example = "123";
int number = example; //this will cause an error!
int number = Integer.parseInt(example); //this will now store
//123 as a primitive
Here is some code to help you get started with this step. This is just an example of what you can do; you do not
have to do it this way!
String[] names = //stores all the names
int[] ages = //stores all the ages
String[] gender = //stores all the genders
String[] hometown = //stores all the hometowns
int[] days = //stores all the days attending
String[] number = //stores all the phone numbers
int i = 0; //keeps track of the current index
for(String line : rows){ //goes through each line
names[i] =
ages[i] =
gender[i] =
hometown[i] =
days[i] =
number[i] =
i++;
}
Step 3: Statistics
Task 1: Display the student names
Task 2: Display the average age of the female children and the average age of the male children
Task 3: Display how many students are from each of the towns with a histogram
Task 4: How much does the daycare make in income in one week?
 1 year olds cost $35 a day
 2 year olds cost $30 a day
 3 year olds cost $25 a day
 4 year olds cost $20 a day
 5 year olds cost $15 a day
Task 5: Give the choice of a student from a list, look up phone number of a user inputted number.
Task 6: Use a sorting algorithm to sort the students in order of their last name. Make sure that when positions
are switched, they are switched for all arrays so the same index applies to the same student across the board.