Core Java · Chapter 4 of 48
Strings
By the end of this chapter you can clean, cut, split, format and compare text, and build long text efficiently. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: First steps
- 6 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
Anjali has a receipt from the NDLS counter. The customer's name came out with stray spaces and odd capitals, the items run into each other, and the prices do not line up. Customers photograph receipts and post them, so she wants them neat: names tidied, items listed cleanly, columns aligned.
So far you can only join text with +. That cannot remove a space, find where a station code ends, or pad a name to a fixed width, and joining a long receipt piece by piece makes a new string every time. Text is a String, and String has methods for all of this.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Receipt.java: clean up a customer's name
In Receipt.java, make cleanName(raw) return the name without the spaces around it, with its first letter in upper case and every other letter in lower case. A one-letter name is just that letter in upper case. A name that is empty or only spaces returns Guest.
For example, cleanName(" aNJali ") returns Anjali, cleanName("MEERA") returns Meera, cleanName(" k ") returns K, and cleanName(" ") and cleanName("") both return Guest.
- Task 2
Receipt.java: read the station code from a ticket
In Receipt.java, make station(ticketId) return everything before the first dash of the ticket number. A ticket number with no dash at all is returned whole.
For example, station("SBC-2026-0042") returns SBC, station("NDLS-2026-7") returns NDLS and station("CSMT") returns CSMT.
- Task 3
Receipt.java: tidy a messy order
In Receipt.java, make tidyOrder(order) split the order at every comma, remove the spaces around each item, and join the items back with a comma and one space between them. One item with no comma comes back stripped. An order that is empty or only spaces returns the empty string.
For example, tidyOrder("masala chai , cutting chai,bun maska ") returns masala chai, cutting chai, bun maska and tidyOrder(" bun maska") returns bun maska.
- Task 4
Receipt.java: format one receipt line
In Receipt.java, make line(item, qty, price) return one receipt line: the item padded with spaces on the right to 14 characters, then a space, an x and the quantity, then two spaces, the rupee sign ₹ and the amount, which is qty times price.
For example, line("Masala chai", 2, 40) returns Masala chai x2 ₹80 and line("Cutting chai", 10, 25) returns Cutting chai x10 ₹250.
- Task 5
Receipt.java: build the whole receipt
In Receipt.java, make receipt(station, lines) return the whole receipt as one String: HEADER with the station code filled in, then each line from the list followed by a line break, then a line of 24 dashes followed by a line break. A receipt with no lines is the header and the dashes.
For example, receipt("SBC", an empty list) returns Tapri Counter, Station: SBC and 24 dashes, each on its own line, with a line break at the end.
- Task 6 · interview question
Interview: Receipt.java, predict == and equals on strings
In Receipt.java, make predictComparisons() return the lines the code in its comment prints, one string per line, in order, each one true or false in lower case.
The check runs the same code and compares your list with what Java really printed.
What you will learn
- String methods: strip, case changes, indexOf, substring, and the rule that a String never changes.
- split and join: cutting text into parts and gluing parts back.
- formatted and text blocks: padded columns and multi-line text.
- StringBuilder, == and equals: building text in a loop, and comparing text correctly.
More in First steps
Build "Strings" 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.