Solved

occurence count of character in a string

Posted on 2014-10-09
7
230 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 52
How to convert from xls to xlsx using java 7 85
collection output issue 9 59
IBM TS2900 (3572) Tape Autoloader Java? 12 58
This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 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