Solved

occurence count of character in a string

Posted on 2014-10-09
7
212 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
Thanx 4 axxepting
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
array11 challenge 16 50
Change to event 1 72
sites similar to codingbat to improve coding hanson skills 3 19
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

13 Experts available now in Live!

Get 1:1 Help Now