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" ?

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.