Solved

occurence count of character in a string

Posted on 2014-10-09
7
222 Views
Last Modified: 2014-10-10
Hi,

I am trying below challenge

http://codingbat.com/prob/p173784

I wrote as below

public boolean stringE(String str) {

int counter = 0;
for( int i=0; i<str.length(); i++ ) {
    if( str.charAt(i) == 'e' ) {
        counter++;
    } 
    if(1<=counter<=3){
    return true;
    }
    return false;
}
  
}

Open in new window


I am getting error as

Compile problems:


Error:      if(1<=counter<=3){
         ^^^^^^^^^^^^^
The operator <= is undefined for the argument type(s) boolean, int


see Example Code to help with compile problem


How to fix this error and improve my program to make it work. Please advise
0
Comment
Question by:gudii9
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40372050
You can get rid of the error by using :

 if(1<=counter && counter<=3){

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40372060
Error is gone. What is the difference between

if(1<=counter && counter<=3){

and

if(1<=counter<=3){

public boolean stringE(String str) {

int counter = 0;
for( int i=0; i<str.length(); i++ ) {
    if( str.charAt(i) == 'e' ) {
        counter++;
    } 
    if(1<=counter && counter<=3){
    return true;
    }
    return false;
}
  return false;
}

Open in new window


My code looks like above i have few test cases failing as below

Expected      Run            
stringE("Hello") → true      false      X         
stringE("Heelle") → true      false      X         
stringE("Heelele") → false      false      OK         
stringE("Hll") → false      false      OK         
stringE("e") → true      true      OK         
stringE("") → false      false      OK         


Please advise
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40372073
Change it to :

for( int i=0; i<str.length(); i++ ) {
    if( str.charAt(i) == 'e' ) 
        counter++;
    }
    return(1<=counter && counter<=3);

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:gudii9
ID: 40372080
public class Test10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		stringE("hello");
	}

	
	public static boolean stringE(String str) {

		int counter = 0;
		for( int i=0; i<str.length(); i++ ) {
		    if( str.charAt(i) == 'e' ) {
		        counter++;		    
		    if(1<=counter && counter<=3){
		    return true;
		    }
		  //  return false;
		}
		  return false;
		}
		return false;
	}
}

Open in new window



i wrote as above. I wonder what is wrong in that. Please advise
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 300 total points
ID: 40372097
the issues with your code is that on the first run i.e. for x=0 it will evaluate the counter in the if statement. Your issues is that you put the evaluation of the counter in the for statement which should loop thru the entire string.. Take it outside the for statement and then it will work..

public boolean stringE(String str) {

		int counter = 0;
		for( int i=0; i<str.length(); i++ ) {
		    if( str.charAt(i) == 'e' ) {
		        counter++;
		    }}
//here the for statement is completed and counter has the final value


		    if(1<=counter && counter<=3){
		    return true;
		    }
	
		  return false;
		//return false;
	}

Open in new window

0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 200 total points
ID: 40372349
>> What is the difference between
>> if(1<=counter && counter<=3){
>> and
>> if(1<=counter<=3){

The latter is NO (valid) java code.

A java condition expresses one condition or a concatenation of more conditions using && and ||

You want to express two conditions:
1) that your counter is 1 or bigger
AND
2) your counter is 3 or less

so, you have to do it as you do it in English:

if ( counter >= 1 && counter <= 3 ) {
}

>> i wrote as above. I wonder what is wrong in that. Please advise
The main problem is that you check the condition
if ( counter >= 1 && counter <= 3 )
for EACH run of the loop. (ie. for each character in the string)
You have to run through the complete loop (ie. check ALL characters in the string) and AFTER that, you have to check your condition.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40372973
Thanx 4 axxepting
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 29
web application structure 18 76
Configure a Bean in an XML file 4 31
servlet example 17 28
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now