Lesson Overview
Collect, validate and process simple data using arrays, strings and scanner-style input.
Portfolio focus: Store five marks in an array.
Starter: think before typing
Before running this data handling example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `0: ADA 1: GRACE 2: ALAN`; predict how the focus line helps produce that evidence.
Learning Objectives
- Use arrays for fixed-size collections.
- Process strings with common Java methods.
- Read and validate user input.
- Explain index errors and empty-input problems.
Learning Outcomes
- By the end of the lesson, you can use arrays for fixed-size collections.
- By the end of the lesson, you can process strings with common Java methods.
- By the end of the lesson, you can read and validate user input.
- By the end of the lesson, you can explain index errors and empty-input problems.
Why this idea exists
Collections and strings matter because useful programs rarely work with one value at a time. They process names, files, marks, messages, records and many other sequences of data.
Arrays reflect a low-level idea: values stored in ordered positions that can be accessed by index. Strings add another layer, representing text as a sequence of characters with operations such as searching, slicing and comparison.
Many beginner bugs come from forgetting that program input is messy. Degree-level programmers treat input as untrusted until it has been checked, converted and handled in a way the rest of the program can rely on.
Deep dive
Mechanism in this example
The important mechanism is visible around `System.out.println(index + ": " + names[index].toUpperCase());`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.
Design pressure
Many beginner bugs come from forgetting that program input is messy. Degree-level programmers treat input as untrusted until it has been checked, converted and handled in a way the rest of the program can rely on.
Failure mode to watch
For Arrays, Strings and Input, deliberately disturb the assumption behind `System.out.println(index + ": " + names[index].toUpperCase());`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the data handling design.
Extension step
Extend the example by doing this: Calculate the mean mark. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.
Portfolio standard
The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: reject an empty name before saving it.
Lesson visual

Type this and run it
Create ArraysStringsAndInputDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.
public class ArraysStringsAndInputDemo {
public static void main(String[] args) {
String[] names = {"Ada", "Grace", "Alan"};
for (int index = 0; index < names.length; index++) {
System.out.println(index + ": " + names[index].toUpperCase());
}
}
}Build and run it with:
javac ArraysStringsAndInputDemo.java && java ArraysStringsAndInputDemoExpected baseline: 0: ADA
1: GRACE
2: ALAN
Run the code in your browser
Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates data handling and compare the new behaviour with the reference output.
0: ADA
1: GRACE
2: ALANLine-by-line explanation
Read the code as a sequence of responsibilities. The focus line for this lesson is System.out.println(index + ": " + names[index].toUpperCase());; the surrounding lines prepare it, use its result or make the behaviour observable.
public class ArraysStringsAndInputDemo {This names the runnable class for the Arrays, Strings and Input example, giving the compiler and JVM one clear unit to build.
public static void main(String[] args) {This is the program entry point. In Arrays, Strings and Input, it keeps the demonstration of data handling in one traceable starting script.
String[] names = {"Ada", "Grace", "Alan"};This introduces names as named state for Arrays, Strings and Input. Later lines can read, update, pass or print that specific value as evidence.
for (int index = 0; index < names.length; index++) {This introduces index as named state for Arrays, Strings and Input. Later lines can read, update, pass or print that specific value as evidence.
System.out.println(index + ": " + names[index].toUpperCase());This prints index + ": " + names[index].toUpperCase() as the observable evidence for Arrays, Strings and Input. The output lets the learner check whether the data handling idea behaved as predicted.
}This closes the innermost Arrays, Strings and Input block, so the immediately preceding method, branch or loop has finished.
}This closes the outer Arrays, Strings and Input structure, returning the reader to the surrounding class or file.
}This closing brace number 3 completes another layer of the Arrays, Strings and Input source structure Java has been checking.
Worked example
From code to explanation
Problem: Use Arrays, Strings and Input to complete a small portfolio-quality step: Store five marks in an array.
Method: Locate the line `System.out.println(index + ": " + names[index].toUpperCase());`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.
Reveal worked answer
The checked run should produce `0: ADA 1: GRACE 2: ALAN`. A strong answer links the result back to data handling: what was created, selected, stored, called or protected, and why that matters for the portfolio task.
Trace the program
Before: Before the key operation, identify the relevant value, object, branch or resource that the data handling concept depends on.
During: Trace `System.out.println(index + ": " + names[index].toUpperCase());` as the Arrays, Strings and Input example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.
After: Compare the run with the expected evidence: `0: ADA 1: GRACE 2: ALAN`.
Change: Now calculate the mean mark, run again, and explain the smallest reason the behaviour changed.
Common misconception
A common mistake in arrays, strings and input is treating the example as a finished answer. For data handling, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?
Quick checks
1. In this Arrays, Strings and Input example, what is the best reason to focus on `System.out.println(index + ": " + names[index].toUpperCase());`?
2. Which evidence is strongest after you edit and rerun this example?
How to study this lesson
For Arrays, Strings and Input, predict how data handling changes the run before you press Run.
Use the first portfolio task as your main edit: Store five marks in an array.
Use the second task as your variation: Calculate the mean mark.
Finish with evidence, not a diary entry: Reject an empty name before saving it.
Portfolio Practice
- Store five marks in an array.
- Calculate the mean mark.
- Reject an empty name before saving it.
Final self-check
Can you explain the key operation?
Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output 0: ADA
1: GRACE
2: ALAN appears.
Can you justify the portfolio evidence?
Your evidence should include the original run, one edited run, and a short note explaining how the edit affected data handling.
Study route
Practise data handling by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.
Next, move into Testing, Debugging and Maintenance and carry forward one improvement from this lesson into the next program.
