Solved

occurence count of character in a string

Posted on 2014-10-09
7
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.
Suggested Courses

635 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