Programming lesson
Mastering Java File I/O, Methods, and Arrays with Movie Review Data
Learn how to read a text file into an array of User objects, then analyze data using methods in Java. This step-by-step tutorial uses a movie review dataset and ties in concepts from real-world apps like IMDb and social media analytics.
Introduction: Why File I/O and Arrays Matter in Java
In today's data-driven world, applications like IMDb, Rotten Tomatoes, and Letterboxd process millions of user reviews daily. Understanding how to read data from files, store it in arrays, and perform calculations using methods is a fundamental skill for any Java developer. This tutorial walks you through a typical assignment: reading a user dataset from a text file, creating an array of User objects, and implementing methods to analyze the data. By the end, you'll be able to handle similar real-world tasks, such as analyzing user demographics for a movie review site or processing survey responses.
Step 1: Defining the User Class
First, create a User class with five private instance variables: userID (int), age (int), sex (String), occupation (String), and zipCode (String). Provide getters, setters, and a toString() method that returns a tab-separated line. This encapsulates each user's data and makes it easy to work with in arrays.
public class User {
private int userID;
private int age;
private String sex;
private String occupation;
private String zipCode;
// Constructor, getters, setters
@Override
public String toString() {
return userID + "\t" + age + "\t" + sex + "\t" + occupation + "\t" + zipCode;
}
}Step 2: Reading the File into an Array of Users
In your driver class (UserDataProcessingDriver), declare an array User[] usersArr = new User[200];. Use a Scanner to read the file line by line. For each line, parse the five tab-separated fields using nextInt(), nextInt(), next(), next(), and nextLine(). Create a new User object and store it in the array. After reading, print all users to verify.
Scanner fileScanner = new Scanner(new File("Assignment-6-user-data.txt"));
int index = 0;
while (fileScanner.hasNextLine() && index < 200) {
int id = fileScanner.nextInt();
int age = fileScanner.nextInt();
String sex = fileScanner.next();
String occupation = fileScanner.next();
String zip = fileScanner.nextLine().trim();
usersArr[index] = new User(id, age, sex, occupation, zip);
index++;
}
fileScanner.close();Step 3: Implementing Analysis Methods
Method 1: avgAge
Calculate the average age of all users. Loop through the array, sum the ages, and divide by the number of users (use a counter to handle nulls).
public static double avgAge(User[] users) {
int sum = 0, count = 0;
for (User u : users) {
if (u != null) {
sum += u.getAge();
count++;
}
}
return count > 0 ? (double) sum / count : 0;
}Method 2: countOccupation
Count how many users have a specific occupation. Pass the array and a target occupation string. Use equalsIgnoreCase() for case-insensitive matching.
public static int countOccupation(User[] users, String occupation) {
int count = 0;
for (User u : users) {
if (u != null && u.getOccupation().equalsIgnoreCase(occupation)) {
count++;
}
}
return count;
}Method 3: avgOccupationAge
Compute the average age of users with a given occupation. Similar to avgAge but filter by occupation.
public static double avgOccupationAge(User[] users, String occupation) {
int sum = 0, count = 0;
for (User u : users) {
if (u != null && u.getOccupation().equalsIgnoreCase(occupation)) {
sum += u.getAge();
count++;
}
}
return count > 0 ? (double) sum / count : 0;
}Method 4: countMaleBelowAge
Return the number of male users whose age is less than a given threshold. Check both sex and age.
public static int countMaleBelowAge(User[] users, int ageLimit) {
int count = 0;
for (User u : users) {
if (u != null && "M".equals(u.getSex()) && u.getAge() < ageLimit) {
count++;
}
}
return count;
}Real-World Connections
Think of this dataset as a simplified version of user data from a movie review platform like IMDb. The same techniques can be applied to analyze user demographics for a new streaming service or even to process survey data from a school project. For instance, you could find the average age of users who are 'students' or count how many male users are under 30. These are common queries in data analysis and business intelligence.
Common Pitfalls and Tips
- File Not Found: Always catch
FileNotFoundExceptionor declarethrowsin the method signature. - Array Size: Use a constant for the array size (e.g.,
final int MAX_USERS = 200;) to avoid magic numbers. - Null Handling: When iterating, check for
nullto avoidNullPointerExceptionif the array isn't full. - Case Sensitivity: Use
equalsIgnoreCase()for occupation matching to handle variations like 'Technician' vs 'technician'.
Testing Your Code
Call each method from main and print the results. For example:
System.out.println("Average age: " + avgAge(usersArr));
System.out.println("Number of students: " + countOccupation(usersArr, "student"));
System.out.println("Average age of students: " + avgOccupationAge(usersArr, "student"));
System.out.println("Male users below 50: " + countMaleBelowAge(usersArr, 50));Conclusion
By completing this tutorial, you've practiced essential Java skills: file I/O, object-oriented programming, arrays, and method creation. These building blocks are used in countless applications, from analyzing social media data to processing financial transactions. Keep experimenting with different datasets—try analyzing a CSV file from Kaggle or even your own school's survey data. The possibilities are endless!