Method Overloading check() String & Char Program (ICSE Java)

⚑ Direct Answer & Key Takeaway

In the ICSE board check() overloading problem, a class defines two void methods named check: check(String str, char ch) counts how many times ch appears in str, while check(String s1) converts s1 to lowercase and prints every vowel. Because one version accepts two arguments and the other accepts one, Java resolves each call unambiguously at compile time using length() and charAt() traversal.

1. Scanning Characters with length() and charAt()

Finding how many times ch appears inside str requires inspecting every index from 0 up to str.length() - 1. In Java, str.charAt(i) extracts the character at index i as a primitive char, which you compare directly against ch using the equality operator == rather than .equals(). Each match increments an integer counter count initialized to 0 before the loop starts. Remember that 'S' and 's' have different Unicode values (83 versus 115), so preserve exact case matching unless the ICSE question explicitly asks for case-insensitive counting.

  • Initialize count = 0 before entering the for loop over str.length().
  • Use strict index bounds (0 <= i < str.length()) because string indexing begins at 0.
  • Compare charAt(i) == ch with single quotes around char literals.

2. Extracting Vowels After Lowercase Normalization

To extract only vowels from s1, normalize the entire string first with s1 = s1.toLowerCase();. Because Java String objects are immutable, calling s1.toLowerCase() without assigning the result back to s1 leaves the original uppercase characters unchangedβ€”a common Section B trap. Once s1 is lowercase, check each character c = s1.charAt(i) against 'a', 'e', 'i', 'o', and 'u' using logical OR (||), cutting the condition in half compared to testing ten uppercase and lowercase vowels.

  • Reassign s1 = s1.toLowerCase() because String methods never mutate the original object.
  • Test all five vowels explicitly: c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'.
  • Print vowels inline using System.out.print() and finish with System.out.println().

3. Safe Scanner Input for Strings and Characters

Console programs mixing Scanner tokens often fail when sc.nextLine() reads a leftover newline character right after sc.next() or sc.nextInt(). In our main() method, reading the first string with sc.nextLine(), extracting the single target character via sc.next().charAt(0), and inserting a dummy sc.nextLine() clears the input buffer cleanly before reading s1. Without that buffer-clearing line, s1 receives an empty string "" and prints zero vowels, even though the check(String) method logic itself is completely valid.

  • Read full phrases including spaces using sc.nextLine() rather than sc.next().
  • Consume the leftover newline character via sc.nextLine() after sc.next().charAt(0).
  • Document loop variable i, count, and extracted char c clearly in your VDT.

4. StringCheck.java β€” ICSE Overloaded check() for Char Frequency & Vowels

import java.util.*;

public class StringCheck {
    public void check(String str, char ch) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ch) {
                count++;
            }
        }
        System.out.println("Frequency of '" + ch + "' = " + count);
    }

    public void check(String s1) {
        s1 = s1.toLowerCase();
        System.out.print("Vowels: ");
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                System.out.print(c + " ");
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        StringCheck obj = new StringCheck();

        System.out.print("Enter a string for frequency check: ");
        String str = sc.nextLine();
        System.out.print("Enter character to count: ");
        char ch = sc.next().charAt(0);
        obj.check(str, ch);

        sc.nextLine();
        System.out.print("Enter a string to extract vowels: ");
        String s1 = sc.nextLine();
        obj.check(s1);

        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter a string for frequency check: success
Enter character to count: s
Frequency of 's' = 3
Enter a string to extract vowels: Computer Applications
Vowels: o u e a i a i o

5. CISCE Variable Description Table (VDT) & Dry Run

VariableData TypeExam Purpose & Sample Value
strStringInput word or phrase for frequency search ("success")
chcharTarget character counted inside check(String, char) ('s')
countintAccumulator tracking matching character occurrences (3)
s1StringLowercased string scanned for vowels ("computer applications")
ccharCurrent character extracted by s1.charAt(i) ('o', 'u', ...)
iintLoop index iterating from 0 to length() - 1 (0..6)
🎯 1-on-1 Live Mentorship & Free Study Kit

Ace ICSE String Programs 1-on-1

Practice String manipulation, charAt() loops, and ICSE past-paper check() overloading questions live with an experienced Java tutor.

πŸ’¬ WhatsApp Now
πŸ”’ Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Writing str.charAt(i).equals(ch) instead of ==, which fails compilation because char is a primitive type.
  • Avoid Mistake: Calling s1.toLowerCase(); without assigning it back to s1, leaving uppercase vowels undetected.
  • Avoid Mistake: Using i <= str.length() in the loop condition, throwing StringIndexOutOfBoundsException on the final step.
  • Examiner / Mentor Tip: Remember length() for String uses parentheses, whereas array .length does notβ€”a classic Section A trap.
  • Examiner / Mentor Tip: Do not confuse single quotes 'a' (char) with double quotes "a" (String) during vowel comparisons.
  • Examiner / Mentor Tip: Keep both check() methods void as required by the ICSE 2019 pattern so no return statement is expected.

7. Interactive Concept Check Quiz

Question: What output does obj.check("Banana", 'a') produce in the StringCheck class?

8. Frequently Asked Questions

Why do we use == instead of .equals() when comparing characters in check()?
In Java, charAt(i) returns a primitive char value, not an object. Primitive types such as char, int, and double are compared using ==, whereas .equals() only works on reference types like String. Calling str.charAt(i).equals(ch) will immediately fail compilation in BlueJ with a dereference error.
What happens if we write s1.toLowerCase(); without reassigning s1?
Because Java strings are immutable, s1.toLowerCase() creates a new lowercase String in memory without altering s1. If you omit s1 = s1.toLowerCase();, uppercase vowels like 'A' or 'E' remain uppercase inside s1 and fail lowercase vowel checks such as c == 'a', producing incomplete output.
How do you read a single char safely using Scanner in BlueJ?
Java's Scanner class has no nextChar() method. Instead, call sc.next().charAt(0) to read the next whitespace-delimited token and extract its first character at index 0. Follow it with sc.nextLine() if you plan to read another full sentence later so the newline buffer stays clean.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class