Solved

JAVA Coding: Strings and Loops

Posted on 2013-01-05
6
559 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
netstat -ano | find "8000" and taskkill /f /pid 2984 3 84
factorial example 4 50
iterator/ListIterator approach 17 39
Unhandled exception type Exception 18 43
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

830 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