Turning CSV Data into Arrays and Doing Simple Analysis in Java
*(aka: “Yes, this is programming, but you won’t hate it”)
Like all our tutorials, this one is written for two kinds of people:
🧢 Teenager who says:
“Java is for boring nerds. I’m gonna become a designer and make cool stuff on Figma instead.”
📊 Reality check: Nerds get paid to automate the world. You still haven’t figured out Google Drive permissions. Let’s fix that.
🧑💼 Grown adult, currently burned out, working a job they lowkey hate, and thinking:
“Should I switch to tech? But isn’t coding, like… super hard?”
☕ Answer: It’s not. You’ve learned harder things. You can absolutely do this — and we’re going to prove it with the most boring dataset imaginable: weather.
- 🧢 The teenager who thinks Java is for nerds and dreams of becoming a “UI/UX ninja” without writing a single line of code.
- 🧑💼 The adult who’s burned out at their job and staring at the ceiling at night whispering, “Is it too late to learn programming? I just want to be free…”
This is Java. But don’t worry:
- No classes.
- No objects.
- No collections.
Just arrays, loops, and some logic that makes actual sense.
Let’s go.
🗂️ What Kind of Data Are We Working With?
You get a file from a weather server. It’s in CSV format — plain text with commas separating the values.
Here’s an example from weather.csv a CSV file is basically an electronic sheet of records that’s been dropped into a digital inbox — your program’s job is to open that inbox and read the sheet line by line):
2026-01-10 12:00,-2.5,4.2
2026-01-10 12:10,-2.8,5.1
2026-01-10 12:20,-3.6,6.4
2026-01-10 12:30,-1.9,3.8
Each line means:
timestamp, temperature, wind speed
To do anything with this in Java, we have to read it as text, extract the numbers, and then store them in arrays.
What Arrays Will We Use?
We’ll keep it simple:
One array for temperatures, one for wind speeds.
(We’ll skip the timestamps for now — less distraction.)
temperatures[] → stores temperature data
windSpeeds[] → stores wind speed data
The index connects everything — temperatures[2] and windSpeeds[2] refer to the same measurement.
Step 1: Count How Many Lines Are in the File
Before we create our arrays, we need to know how many measurements there are.
import java.io.BufferedReader;
import java.io.FileReader;
public class WeatherAnalysis {
public static void main(String[] args) throws Exception {
// Path to the CSV file
String filePath = "weather.csv";
// Count the number of lines
int linesCount = 0;
// Open the file
BufferedReader reader = new BufferedReader(new FileReader(filePath));
// Read line by line
while (reader.readLine() != null) {
linesCount++; // increase the count
}
reader.close(); // don’t forget to close the file
Nothing’s being saved here — we’re just counting lines so we know how big the arrays should be.
Step 2: Create the Arrays
Now we create two arrays with the size we just counted:
// Create an array for temperatures
double[] temperatures = new double[linesCount];
// Create an array for wind speeds
double[] windSpeeds = new double[linesCount];
Each element will hold one number from the file.
Step 3: Read the Data and Fill the Arrays
We open the file again, but this time we’ll actually extract and store the data.
reader = new BufferedReader(new FileReader(filePath));
String line; // one line from the file
int index = 0; // index for the arrays
while ((line = reader.readLine()) != null) {
// Split the line by commas
String[] parts = line.split(",");
// parts[0] — timestamp (ignored for now)
// parts[1] — temperature
// parts[2] — wind speed
temperatures[index] = Double.parseDouble(parts[1]);
windSpeeds[index] = Double.parseDouble(parts[2]);
index++; // move to the next position
}
reader.close();
This is where the text becomes numbers. Now we can actually do things with the data.
Step 4: Find the Minimum and Maximum Temperature
Let’s find out what the lowest and highest temperature values are:
double minTemp = temperatures[0];
double maxTemp = temperatures[0];
for (int i = 1; i < temperatures.length; i++) {
if (temperatures[i] < minTemp) {
minTemp = temperatures[i];
}
if (temperatures[i] > maxTemp) {
maxTemp = temperatures[i];
}
}
System.out.println("Min temperature: " + minTemp);
System.out.println("Max temperature: " + maxTemp);
This is basic — but extremely useful in real life, especially when dealing with data sets bigger than four lines.
Step 5: See If Wind Increases When Temperature Changes Rapidly
Here’s where it gets a little spicy:
Let’s check if wind speed increases when temperature suddenly changes by 2°C or more.
for (int i = 1; i < temperatures.length; i++) {
// Difference between current and previous temp
double tempDifference = temperatures[i] - temperatures[i - 1];
// Difference in wind speed
double windDifference = windSpeeds[i] - windSpeeds[i - 1];
// Is it a sudden temp change?
if (Math.abs(tempDifference) >= 2.0) {
System.out.println(
"Sudden temperature change: " + tempDifference +
" | wind speed change: " + windDifference
);
}
}
}
}
Now we’re connecting data points. You’re not just logging numbers — you’re starting to analyze.
🤓 Why Use Arrays for This?
Fair question. Here’s why:
- Arrays are simple — they let you focus on the logic, not Java’s quirks.
- They help you understand index-based thinking, which is core to all programming.
- You’ll use fancier tools later (like
List,Map, or even Pandas in Python),
but arrays force you to think through the data.
Takeaways
- CSV files are text — you have to extract the numbers manually.
- Arrays are your first tool to organize that data.
- One array = one type of data.
- Same index = same row/measurement.
- With just arrays, you can:
- find min/max
- compare values
- analyze trends
If you understood this, you’ve already taken your first real step into data programming.
📜 Full Program Code (with comments)
import java.io.BufferedReader;
import java.io.FileReader;
public class WeatherAnalysis {
public static void main(String[] args) throws Exception {
/* -----------------------------------------------
1. Path to the CSV file
----------------------------------------------- */
String filePath = "weather.csv";
/* -----------------------------------------------
2. Count how many lines are in the file
----------------------------------------------- */
int linesCount = 0;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
while (reader.readLine() != null) {
linesCount++;
}
reader.close();
/* -----------------------------------------------
3. Create the arrays
----------------------------------------------- */
double[] temperatures = new double[linesCount];
double[] windSpeeds = new double[linesCount];
/* -----------------------------------------------
4. Fill the arrays with data from the file
----------------------------------------------- */
reader = new BufferedReader(new FileReader(filePath));
String line;
int index = 0;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
temperatures[index] = Double.parseDouble(parts[1]);
windSpeeds[index] = Double.parseDouble(parts[2]);
index++;
}
reader.close();
/* -----------------------------------------------
5. Find min and max temperature
----------------------------------------------- */
double minTemperature = temperatures[0];
double maxTemperature = temperatures[0];
for (int i = 1; i < temperatures.length; i++) {
if (temperatures[i] < minTemperature) {
minTemperature = temperatures[i];
}
if (temperatures[i] > maxTemperature) {
maxTemperature = temperatures[i];
}
}
System.out.println("Min temperature: " + minTemperature);
System.out.println("Max temperature: " + maxTemperature);
/* -----------------------------------------------
6. Check if wind speed reacts to sudden temp changes
----------------------------------------------- */
for (int i = 1; i < temperatures.length; i++) {
double temperatureDifference = temperatures[i] - temperatures[i - 1];
double windSpeedDifference = windSpeeds[i] - windSpeeds[i - 1];
if (Math.abs(temperatureDifference) >= 2.0) {
System.out.println(
"Sudden temperature change: " + temperatureDifference +
" | wind speed change: " + windSpeedDifference
);
}
}
}
}
A Reality Check
If you thought working with project stakeholders would be as entertaining as reading this article — here’s your dose of reality:
a technical specification for the exact same program.
📄 Technical Specification: Weather Data Analysis from CSV
Objective:
Develop a Java console application that analyzes weather data provided in CSV format.
Input Data Format:
A file named weather.csv containing lines in the following format:<timestamp>,<temperature>,<wind_speed>
Example line: 2026-01-10 12:00,-2.5,4.2
Values are comma-separated and sorted chronologically.
Functional Requirements:
- The program must read the file and count the number of lines.
- Based on the line count, create two arrays:
double[] temperatures— for temperature valuesdouble[] windSpeeds— for wind speed values
- Re-read the file and:
- Parse temperature and wind speed values from each line
- Populate the corresponding arrays
- After loading the data:
- Determine and print the minimum and maximum temperature
- Analyze consecutive data points to detect significant temperature changes (threshold: 2.0°C)
- For each such case, print:
- the temperature difference
- the corresponding change in wind speed
- For each such case, print:
Constraints:
- Use only basic Java constructs (
double[],for,if,BufferedReader) - Do not use collections, model classes, external libraries, or third-party dependencies
- Ignore the
timestampfield; no date/time processing is required
Expected Output:
Console output showing the minimum and maximum temperatures, along with any detected instances of sudden temperature changes and related wind speed variations.
