Exercise 5 of 10 · Reading Input
Airport Security Line Status
What you will make
A program that reads how many minutes a traveler waited in a security line and prints a status label for it.
The one new idea: Reading a line from the console and parsing it into a number
Real programs usually react to input instead of only using values baked into the code — reading from the keyboard is the first step toward interactive programs.
Go straight to the code ↓So far every value in these programs has been hardcoded. Real programs usually react to something typed in instead. Console.ReadLine() reads one line of text from the input and returns it as a string (or null if there's nothing to read, which is why it's paired with ?? "fallback"). Since it's always text, turning it into a number you can compare or do math with requires int.Parse(...), which converts a numeric string like "12" into the actual int value 12. It's easy to reach for some other member that happens to also return an int — like a string's .Length — without noticing that it means something completely different: a character count, not the value the text spells out.
Worked example
Here's a program reading how many guests are coming to a potluck:
string input = Console.ReadLine() ?? "6";
int guestCount = int.Parse(input);
int chairsAvailable = 8;
Console.WriteLine($"Guests: {guestCount}");
if (guestCount > chairsAvailable)
{
Console.WriteLine("Need more chairs");
}
else
{
Console.WriteLine("Enough chairs");
}Guests: 6
Enough chairsWith no input piped in, Console.ReadLine() returns null, so the ?? "6" fallback kicks in and input becomes "6" — then int.Parse turns that text into the number 6 so it can be compared with chairsAvailable. Notice guestCount holds 6, the value the text represents — not 1, which is how many characters are in "6".
Your turn
The starter code reads a traveler's security line wait time and is supposed to print a status label based on how long it was. It reads the input correctly and has a sensible fallback, but the next line sets waitMinutes from input.Length — the number of characters in the string — instead of converting the text to the number it represents. With the default input "12", that means waitMinutes ends up as 2 (two characters) instead of 12. Fix that line so it actually parses the text into a number.
If something goes wrong
If the wait time that prints looks suspiciously small — like 1 or 2 — no matter what the input text says, that's the .Length bug: you're counting characters instead of converting the value. Since .Length is also an int, this mistake compiles cleanly and won't show up as an error, so the fix is to swap it for int.Parse(input) rather than to chase a compiler message. If the program compiles and runs but the status label still looks wrong after fixing the parse, double-check that the three conditions (<= 10, <= 25, and the else) don't overlap or skip any range — trace a couple of example values through them by hand.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Using a string's .Length (or another int-returning member) instead of actually converting its text to the number it represents.
- `.Length` counts characters — for the text "12" that's 2, not the number twelve — so the result type-checks but means something completely different from the value you wanted.
- Forgetting the ?? "fallback" after Console.ReadLine().
- Without it, the program can crash or behave unpredictably whenever there's no input available to read.
- Trying to compare or do math on the raw string instead of the parsed number.
- Text comparisons don't behave like numeric ones, so parsing has to happen first.
Want a blank editor instead? Open the C# playground.