Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java continue statement

Hi,

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
package com.solution;

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.");
    }
}

Open in new window

Found 9 p's in the string.

How below printed 35 when i comment continue where as above printed 9
package com.solution;

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.");
    }
}

Open in new window


Found 35 p's in the string.

peter piper picked a " + "peck of pickled peppers

when i counted above it has 49 characters including spaces though

please advise
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

So your code is counting every character that isn't a P.

including free space?
Avatar of gudii9

ASKER

peter piper picked a " + "peck of pickled peppers

when i count i am counting as 40 manually though. which also matches below calculation
 as 49-9=40
Don't count the " + " characters; they're not part of the string. (Print it out and see.)
SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

package com.solution;

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("max is " + max);
        System.out.println("Found " + numPs + " p's in the string.");
    }
}

Open in new window


max is 44
Found 35 p's in the string.


i wonder why max says 44 so 44 is 35 plus 9(ie nine P's) total 44 of all characters without +?


why they used + in the below String
peter piper picked a " + "peck of pickled peppers
just to show String concatenation?
Probably.
Avatar of gudii9

ASKER

" + "

Open in new window


even space before and after plus also i should not count to get 44 looks.

Otherwise count coming around 47
Print the searchMe string and count the characters. The plus sign and the spaces are not part of the string; they're Java syntax.
Avatar of gudii9

ASKER

Print the searchMe string and count

right that is max which is 44 .


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

            // process p's
            numPs++;
        }

Open in new window



above code technically  mean

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

            // process p's
            numPs++;
        }

Open in new window


as we are checking searchMe.charAt(i) != 'p' but them adding numPs by one as continue not there to skip that particular iteration of for loop