PDA

View Full Version : Problem with idenifying characters in a string


chevanater
12-05-2006, 04:11 AM
My code needs to look for all occurances of p in string s and mark a ^ below the a. My code compiles but places no arrows. Anyone know what is wrong?

public class string1
{
public static void main(String[] args)
{
String s ="abcrdabcaabc";
String p = "abc";
System.out.println(s);

for(int i=0; i <s.length();i++)
{
if (s.substring(i) == p)
System.out.print("^");
else
System.out.print(" ");
i++;
}

}
}

Spookster
12-05-2006, 04:32 AM
A couple of things first. This seems likely a homework assignment. We can help with questions but be warned we will not do the work for you. Also you should always point out what language you are using. Obviously this is Java. Also use code tags when posting code. Formats it better so it is easier to read


public class string1
{
public static void main(String[] args)
{
String s ="abcrdabcaabc";
String p = "abc";
System.out.println(s);

for(int i=0; i <s.length();i++)
{
if (s.substring(i) == p)
System.out.print("^");
else
System.out.print(" ");
i++;
}

}


As to why this isn't working... As you iterate through the string s you are comparing the string abc to each individual character of string s. Obviously a single character of s will never match the string abc.

Aradon
12-05-2006, 06:34 AM
A couple of things first. This seems likely a homework assignment. We can help with questions but be warned we will not do the work for you. Also you should always point out what language you are using. Obviously this is Java. Also use code tags when posting code. Formats it better so it is easier to read


public class string1
{
public static void main(String[] args)
{
String s ="abcrdabcaabc";
String p = "abc";
System.out.println(s);

for(int i=0; i <s.length();i++)
{
if (s.substring(i) == p)
System.out.print("^");
else
System.out.print(" ");
i++;
}

}


As to why this isn't working... As you iterate through the string s you are comparing the string abc to each individual character of string s. Obviously a single character of s will never match the string abc.

I don't want to disagree with the famous spookster but I have to. Sorry Spook.

No, he's not comparing characters at all actually. Substring takes the first index and then returns the rest of the string.


"unhappy".substring(2);

Would return "happy"

No, the problem lies in two fold

First off you need to know a little bit about Strings. Strings are not like characters or integers. They cannot be qualified by an == sign to try and get equality. When you do == on a String you are seeing if the pointers are the same, not if the contents inside the pointers is the same.

There are several string methods you can use. My personal favoriate is the equalsIgnoreCase

So something like


String dur = "dur";
String dur2 = "dur";
return dur == dur2;


Would return false. Instead we want


String dur = "dur";
String dur2 = "dur";
return dur.equalsIgnoreCase(dur2);


Which would return true.

Second!

The for loop. It is true that a for loop is like an extended while loop. You can even do a while loop if you'd like with


for( ; something > something ; )


However when you use a for loop you usually have three elements. The first is the indexing number. The second is the boolean comparision that you are waiting for, and the third is the action taken on the indexing number or something of that sort. So..


for(int i = 0; i < 2; i++)
System.out.println(i);


would print out

0
1


The number i starts at 0, the boolean condition is tested. It is true, then it goes into the for loop, does what it needs to do. At the end, i is incremented by one number. If I were to do this:


for(int i = 0; i < 2; i++)
{
System.out.println(i);
i++;
}


It would print out


0


Why? Because the i is changed inside the for loop and inside the incremental condition!

So, those are the two major problems with your for loop! The algorithm is also needing a change however that is something to ponder. I suggest looking at the String Class and seeing the differences between substring, startsWith, endsWith, etc.

String API (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html)

Hope that helps

Spookster
12-05-2006, 01:31 PM
Aradon, you are absolutely right. Don't know what I was thinking there.