Solved

JAVA Coding: Strings and Loops

Posted on 2013-01-05
6
557 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why my table column Id is not passed to java object? 4 39
hibernate jars 4 29
runtime exception 2 28
null output 3 25
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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