Solved

java continue statement

Posted on 2016-09-06
10
155 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

840 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