Solved

java continue statement

Posted on 2016-09-06
10
139 Views
Last Modified: 2016-09-06
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
0
Comment
Question by:gudii9
  • 5
  • 4
10 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
ID: 41786437
Commenting out that line makes the code act like this:

            if (searchMe.charAt(i) != 'p')
               numPs++;

Open in new window


So your code is counting every character that isn't a P. This demonstrates why it can be a good idea to wrap if blocks in braces always, even when there's only one line.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786451
So your code is counting every character that isn't a P.

including free space?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786454
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 14

Expert Comment

by:CPColin
ID: 41786457
Don't count the " + " characters; they're not part of the string. (Print it out and see.)
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41786492
CPColin is right. Please use
System.out.println("max is " + max);

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41786542
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?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41786552
Probably.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786558
" + "

Open in new window


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

Otherwise count coming around 47
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41786559
Print the searchMe string and count the characters. The plus sign and the spaces are not part of the string; they're Java syntax.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41786569
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
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
This is about my first experience with programming Arduino.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question