gudii9
asked on
java continue statement
Hi,
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
How below printed 35 when i comment continue where as above printed 9
Found 35 p's in the string.
when i counted above it has 49 characters including spaces though
please advise
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.");
}
}
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.");
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.");
}
}
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.
ASKER
" + "
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.
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++;
}
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++;
}
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
ASKER
including free space?