Core Java · Chapter 9 of 48
Reading input
By the end of this chapter you can read numbers, words and whole lines with a Scanner, check text before you trust it, and write a command loop that survives whatever people type. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
The CSMT counter has a keyboard but no buttons yet, so the staff type commands: add masala chai 2, total, quit. Ravi watched a new hire type add masala chai two, and the whole till crashed in the middle of the rush. "People type anything. It has to keep going."
Until now every value came from your own code. Typed input is text that you did not write: a word where you expected a number, a stray space, an empty line. Read it carelessly and the program throws, or quietly reads the wrong thing.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Till.java: read a quantity and then an item name
In Till.java, make readOrder(in) read a quantity from one line and the item name from the next line of the Scanner, and return the quantity, then " x ", then the whole item line. Any lines after the item stay unread.
For example, with the input 2, a line break, masala chai and a line break, it returns 2 x masala chai; with 10, bun maska and then extra line, it returns 10 x bun maska.
- Task 2
Till.java: skip words until the customer types a number
In Till.java, make readCups(in) skip every token of the Scanner that is not a whole number and return the first one that is. When the input runs out first, or is empty, it returns -1. A decimal such as 2.5 is not a whole number, so it is skipped too.
For example, two cups please and then 3 on the next line returns 3, 5 returns 5, 2.5 four 4 7 returns 4, no idea returns -1 and empty input returns -1.
- Task 3
Till.java: turn typed text into rupees, or -1
In Till.java, make parseAmount(text) return the text as whole rupees, 0 or more, ignoring spaces around it. Anything else returns -1 and must not throw: a word, a decimal, a negative number, empty text, or a number too big for an int.
For example, 40 and space 40 space return 40 and 0 returns 0; forty, 40.5, -5, the empty string and 99999999999 return -1.
- Task 4
Till.java: run the command loop for add and total
In Till.java, make run(in) read the Scanner line by line until the input ends. Strip each line and skip blank ones. The first word is the command. add ITEM QTY, where the item can have spaces and the quantity is the last word, adds the quantity times the item's price from priceOf to a running total and prints Added QTY x ITEM. total prints Total: ₹ and the total so far, which starts at 0. Each printed line ends with a line break.
For example, add masala chai 2, add bun maska 1 and total print Added 2 x masala chai, Added 1 x bun maska and Total: ₹130. total, then add cutting chai 3 typed with spaces around it, then total print Total: ₹0, Added 3 x cutting chai and Total: ₹75.
- Task 5
Till.java: make the command loop stop at quit and handle bad lines
In Till.java, make run(in) also handle these lines, checking add in this order. add with only one word after it, or none, prints Usage: add ITEM QTY. add whose quantity is not a whole number of 1 or more prints Bad quantity: and the quantity word. add with an item that is not on the menu prints Not on the menu: and the item. quit prints Bye. Total: ₹ and the total, and reads no more lines. Any other command prints Unknown command: and the stripped line. A bad line adds nothing, and input that ends without quit just stops, with no Bye line.
For example, dance, add masala chai two, add masala chai 0, add filter coffee 1, add 2, a blank line, add bun maska 1, quit, add masala chai 5 and total print Unknown command: dance, Bad quantity: two, Bad quantity: 0, Not on the menu: filter coffee, Usage: add ITEM QTY, Added 1 x bun maska and Bye. Total: ₹50, and nothing after that. quit alone prints Bye. Total: ₹0.
What you will learn
- Scanner: reading tokens, numbers and whole lines from typed text.
- The leftover line break: why a line read after a number comes back empty.
- Checking first: hasNextInt to peek, Integer.parseInt with try and catch to convert.
- A loop over lines: reading until the input ends, skipping blank lines, picking a line apart.
More in First steps
Build "Reading input" yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.