Solved

occurence count of character in a string

Posted on 2014-10-09
7
217 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 58
mockito example issue 8 64
Programatically extract date from website 8 68
arguments to jar 5 17
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
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.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

864 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

21 Experts available now in Live!

Get 1:1 Help Now