Solved

JAVA Coding: Strings and Loops

Posted on 2013-01-05
6
554 Views
Last Modified: 2013-01-05
Hello, I am still new to the Java programming language and am having an issue with a code I have written. If possible, I would like some help with identifying why my code is not working for all instances.

The problem is the following:
Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx".

countXX("abcxx") ¿ 1
countXX("xxx") ¿ 2
countXX("xxxx") ¿ 3

This is my code that I have written:

int countXX(String str) {
  String check = "xx";
  int count = 0;
  for (int i = 0; i < str.length(); i++){
    if (str.substring(i, i+1).equals(check)){
      count++;
    }
    else count = count;
  }
  return count;
}


My code so far only returns 0 as the answer. Thank you for any help you can provide.
0
Comment
Question by:Paul_ATL
  • 3
  • 2
6 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 60 total points
ID: 38747307
String.substring(i, i + 1) is always going to return a string that's one character long. You'll want to use String.substring(i, i + 2) to get a two-character-long string. You'll have to end your looping one character sooner, though, so you don't run past the end of the string.

Also, you don't need that "count = count" line; it does nothing.
0
 

Author Comment

by:Paul_ATL
ID: 38747324
When I change the str.substring length to (i, i+2), I get Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: n (line number:5) for all tests except where the string is empty where n varies depending on the string values.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 140 total points
ID: 38747390
This is a good start :

Boyer-Moore-Horspool Algo
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:krakatoa
ID: 38747425
Something less complicated might be :
 
class FindString {

static String searchstring = new String("xxxxxxxx");
static String keystring = "xx";
static int counter =0;

public static void main(String[] args){


while(searchstring.indexOf(keystring,counter)>-1){

counter++;

}
System.out.println(counter);

}


}

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 38747457
When I change the str.substring length to (i, i+2), I get Exception:java.lang.StringIndexOutOfBoundsException.

Right. Like I said, you need to end your loop one iteration sooner. That is, "i + 1" has to be less than the length of the string, instead of what you have now.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38747669
The code I posted previously was incorrect. I think this should work :

class FindString {

static String searchstring = new String("xxxxxyyxxxx");
static String keystring = "xxx";
static int counter =0;
static int found=0;
public static void main(String[] args){

	while(counter<=(searchstring.length()-keystring.length())){
		if(searchstring.regionMatches(false, counter, keystring,0,keystring.length())){
		found++;
		}
		counter++;
	}

System.out.println(found);
}


}

Open in new window

0

Featured Post

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!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

759 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

23 Experts available now in Live!

Get 1:1 Help Now