Bird
Raised Fist0
DSA Javaprogramming~15 mins

String Traversal and Character Access in DSA Java - Build from Scratch

Choose your learning style10 modes available
String Traversal and Character Access
📖 Scenario: You are working on a simple text analyzer tool that needs to process a given word by checking each letter one by one.
🎯 Goal: Build a Java program that traverses a string character by character and prints each character on a new line.
📋 What You'll Learn
Create a string variable with a specific word
Use a loop to access each character of the string
Print each character on its own line
💡 Why This Matters
🌍 Real World
Text processing is common in many applications like spell checkers, search engines, and chatbots where each character or word needs to be examined.
💼 Career
Understanding string traversal is fundamental for software developers working with text data, user input validation, and parsing tasks.
Progress0 / 4 steps
1
Create the string variable
Create a string variable called word and set it to the exact value "hello".
DSA Java
Hint

Use String word = "hello"; to create the variable.

2
Create a loop to traverse the string
Add a for loop with an integer variable i starting at 0 and running while i < word.length(). Increment i by 1 each time.
DSA Java
Hint

Use for (int i = 0; i < word.length(); i++) to loop through the string.

3
Access each character inside the loop
Inside the for loop, create a character variable called ch and set it to the character at index i of word using word.charAt(i).
DSA Java
Hint

Use char ch = word.charAt(i); to get each character.

4
Print each character
Inside the for loop, add a System.out.println(ch); statement to print the character stored in ch.
DSA Java
Hint

Use System.out.println(ch); to print each character on its own line.

Practice

(1/5)
1. What does the charAt() method do when used on a Java String?
easy
A. Returns the character at a specified index in the string
B. Returns the length of the string
C. Converts the string to uppercase
D. Splits the string into an array of substrings

Solution

  1. Step 1: Understand the purpose of charAt()

    The charAt() method is designed to access a single character at a given index in a string.
  2. Step 2: Compare with other string methods

    Methods like length() return the string length, toUpperCase() changes case, and split() divides the string. These are different from charAt().
  3. Final Answer:

    Returns the character at a specified index in the string -> Option A
  4. Quick Check:

    charAt() = character access [OK]
Hint: Remember: charAt gets one character by index [OK]
Common Mistakes:
  • Confusing charAt() with length()
  • Thinking charAt() splits the string
  • Assuming charAt() changes string case
2. Which of the following is the correct way to loop through each character of a string str in Java?
easy
A. for (int i = 1; i < str.length(); i++) { char c = str.charAt(i); }
B. for (int i = 0; i <= str.length(); i++) { char c = str.charAt(i); }
C. for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); }
D. for (int i = 1; i <= str.length(); i++) { char c = str.charAt(i); }

Solution

  1. Step 1: Understand string indexing in Java

    String indices start at 0 and go up to length - 1. So loop must run from 0 to less than length.
  2. Step 2: Check loop conditions

    for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); } uses i < str.length(), which correctly stops before index equals length. Options B and D use <= which causes out-of-bounds error. Options C and D start at 1, skipping first character.
  3. Final Answer:

    for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); } -> Option C
  4. Quick Check:

    Loop from 0 to length-1 [OK]
Hint: Loop from 0 to length-1 for string indices [OK]
Common Mistakes:
  • Using <= instead of < causing index error
  • Starting loop at 1 skipping first char
  • Confusing string length with last index
3. What is the output of the following Java code?
String s = "hello";
for (int i = 0; i < s.length(); i++) {
  if (i % 2 == 0) {
    System.out.print(s.charAt(i));
  }
}
medium
A. hello
B. hlo
C. el
D. heo

Solution

  1. Step 1: Identify loop and condition

    The loop runs from i=0 to 4 (length 5). It prints characters only when i is even (0,2,4).
  2. Step 2: Find characters at even indices

    Index 0: 'h', Index 2: 'l', Index 4: 'o'. These characters are printed in order.
  3. Final Answer:

    hlo -> Option B
  4. Quick Check:

    Even indices chars = hlo [OK]
Hint: Print chars at even indices only [OK]
Common Mistakes:
  • Printing all characters ignoring condition
  • Confusing even and odd indices
  • Off-by-one errors in loop
4. Consider this Java code snippet:
String text = "abc";
for (int i = 0; i <= text.length(); i++) {
  System.out.print(text.charAt(i));
}
What error will occur when running this code?
medium
A. StringIndexOutOfBoundsException
B. ArrayIndexOutOfBoundsException
C. NullPointerException
D. No error, prints 'abc'

Solution

  1. Step 1: Analyze loop bounds

    The loop runs from i=0 to i=3 (because of <= and length=3). Valid indices are 0,1,2 only.
  2. Step 2: Identify error cause

    At i=3, charAt(3) is called, which is out of string bounds, causing StringIndexOutOfBoundsException.
  3. Final Answer:

    StringIndexOutOfBoundsException -> Option A
  4. Quick Check:

    charAt index out of range causes StringIndexOutOfBoundsException [OK]
Hint: Loop must use < length, not <= length [OK]
Common Mistakes:
  • Confusing StringIndexOutOfBoundsException with ArrayIndexOutOfBoundsException
  • Assuming no error occurs
  • Thinking NullPointerException will happen
5. Given the string input = "a1b2c3d4", which code snippet correctly extracts and prints only the digits in order?
hard
A. for (int i = 0; i < input.length(); i++) { if (Character.isLetter(input.charAt(i))) System.out.print(input.charAt(i)); }
B. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') System.out.print(input.charAt(i)); }
C. for (int i = 0; i < input.length(); i++) { System.out.print(input.charAt(i)); }
D. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); }

Solution

  1. Step 1: Understand the goal

    We want to print only digits from the string, ignoring letters.
  2. Step 2: Check each option's condition

    for (int i = 0; i < input.length(); i++) { if (Character.isLetter(input.charAt(i))) System.out.print(input.charAt(i)); } uses Character.isLetter(), prints letters. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= 'a' && input.charAt(i) <= 'z') System.out.print(input.charAt(i)); } checks lowercase letters range, prints letters. for (int i = 0; i < input.length(); i++) { System.out.print(input.charAt(i)); } prints all characters. for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); } checks if char between '0' and '9', prints "1234" correctly.
  3. Final Answer:

    for (int i = 0; i < input.length(); i++) { if (input.charAt(i) >= '0' && input.charAt(i) <= '9') System.out.print(input.charAt(i)); } -> Option D
  4. Quick Check:

    Check char between '0' and '9' to find digits [OK]
Hint: Check if char is between '0' and '9' for digits [OK]
Common Mistakes:
  • Printing letters instead of digits
  • Printing all characters without filtering
  • Using wrong character range for digits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard