Ffirst of all, i have just achieved to code a program that counts the chosen letters. ( For example, i wanted it to count how many 'p's exist in the string. ) Now, for example i want to code a console application that extracts only the ones that are not 'p'. How can i do this with "continue" ?

Dani AI

Generated

made the right start by identifying the characters; to print the string with all p characters removed you can either remove them directly (simple), build a new string while scanning (efficient and explicit), or use Java 8+ streams (Unicode-safe). 's "second string" idea is fine — prefer StringBuilder for performance because String is immutable. If the input contains no p (as pointed out), you can skip work early.

Simple literal removal (fast and readable):

String s = "peter piper picked a peck of pickled peppers";
String result = s.replace("p", ""); // removes lowercase 'p' only
System.out.println(result);

Case-insensitive via regex (remove p and P):

String result = s.replaceAll("(?i)p", ""); // (?i) = case-insensitive

Unicode-correct / Java 8+ approach using code points:

String result = s.codePoints()
    .filter(cp -> cp != 'p' && cp != 'P')
    .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
    .toString();

Quick tips and caveats:

  • String.replace is simple and fastest for a single literal. replaceAll interprets a regex, so escape metacharacters if needed.
  • When removing characters in a loop, use StringBuilder and pre-size it (new StringBuilder(s.length())) to avoid repeated reallocations.
  • Decide whether removal should be case-sensitive and whether you need to handle non-BMP characters; prefer codePoints() for full Unicode correctness.

Recommended Answers

All 9 Replies

Post in your ideas or some initial code and try to get started. Then we can make it interactive to get to the final result :)

String s = "i am a p";
if(s.indexOf("p") == -1) //Index of method checks wether the character is present or not
    methodForCountingAllAlphabet(s); // count if p is not present
else
    continue(); // leave the string if p is present. This all the logic u require.
class ContinueDemo {
    public static void main(String[] args) {

        String searchMe 
            = "peter piper picked a " +
              "peck of pickled peppers";
        int max = searchMe.length();
        int numPs = 0;

        for (int i = 0; i < max; i++) {
            // interested only in p's
            if (searchMe.charAt(i) != 'p')
                continue;

            // process p's
            numPs++;
        }
        System.out.println("Found " +
            numPs + " p's in the string.");
    }
}

Here, i just count the numbers of 'p's. But now, i want to print all the values in the string except 'p' s ? Can you please help me do this ? :)

I did not understand why you wrote -1 ?

GeekTool: take a look at the java api's, check what the indexOf method does, and explain why you think he used -1 as comparator.

GeekTool: take a look at the java api's, check what the indexOf method does, and explain why you think he used -1 as comparator.

If the character you passed in the indexOf method does not exist in the String, indexOf would return a -1. I understood why -1 is written. However, does the code that Majestics published print all the letters except 'p'? Does not it just count how many ?

I have given you a hint to reach your goal. I cant solve all of your problem by pasting code. Its against the rule of this forum. In the above code i provided you a way to do some task over that strings which dont have p. So you simply need a method which counts string letter.

I have given you a hint to reach your goal. I cant solve all of your problem by pasting code. Its against the rule of this forum. In the above code i provided you a way to do some task over that strings which dont have p. So you simply need a method which counts string letter.

Thanks, i have just undestood how to overcome this problem. : )

depends on what the method he calls does..
what you can do is create a second String object, initial value "".
iterate over the characters in your original string, and if it's not p, add it to your seconds string.

after that loop has finished, print your second string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.