AP CSA 1.14 Debugging: Strings Never Change

Unit 1: Using Objects and Methods · Lesson 1.14 · Debugging

Strings Never Change

Calling Instance Methods. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

toUpperCase does not shout at the String you called it on. It builds a brand new String and hands it back, leaving the original exactly as it was, because a String in Java can never be modified once it exists. Every String method returns; none of them edit.

What is wrong with it

  1. The uppercase conversion is called as a bare statement, so the new String it produced is discarded and the original is printed unchanged. Assign the result back.
  2. The trim and the replace are chained but the chain is also discarded. Capture the result of the whole chain into the variable.
  3. The length line is correct, and it is worth noticing that length is the one call here that does not try to change anything.

The program reads

A single word, possibly with surrounding spaces on its line.

The program should print

Three lines: the word in uppercase, the word with every letter a replaced by the digit 4, and the length of the original word.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
banana
Example 1 output
BANANA
b4n4n4
6
Example 2 input
Java
Example 2 output
JAVA
J4v4
4

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

Strings are immutable, which is a promise Java makes and never breaks: the characters inside an existing String cannot be altered by anything. So toUpperCase has no way to change your String even in principle. It can only build a new one and return it.

Hint 2

shouted.toUpperCase(); computes the uppercase version, hands it back, and nobody catches it, so it is discarded immediately. The pattern you want is always s = s.someMethod().

Hint 3

This is the same lesson as 1.10, on an object instead of an int. If a method returns a value, ignoring the return value ignores the entire point of the call.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]