Solved

JAVA Coding: Strings and Loops

Posted on 2013-01-05
6
555 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 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

15 Experts available now in Live!

Get 1:1 Help Now