Jump to content

help using loop to compare string?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
If someone could give me a pointer on how to compare characters in a string.

If I have a string of character in a String varible strText. If I want to count how many 'a' characters are in stored in the string.

I have a counter declared as int count_a and it's initialized to zero.

Once I have a line of text in the strText string.

My loop looks like this:

[FONT=Courier New][/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]for[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] ([/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]int[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] i = 0; i <= strText.length()-1; i++)
{ 
[/FONT][FONT=Courier New][COLOR=#941edf][FONT=Courier New][COLOR=#941edf]    if[/COLOR][/FONT][/COLOR][/FONT][FONT=Courier New] (strText(i).equals("a"[/FONT][FONT=Courier New]))
   { 
   count_a++;
   } [/FONT]
[FONT=Courier New]}

[/FONT]

I get cannot find symbol error from compiler. I know this isn't the right way to set it up but I'm trying to use the equals command to compare the charaters in the string to see how many of the characters are an 'a'.

#2
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
search google for Java String interface. You're trying to access the character at index i like in an array. In java its done by a function called charAt I think. Also if you want to pass a character as parameter, use ' ', and not " ". I mean compare it to 'a' and not to "a".

#3
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I read through the chapter on conditions and loops in my text and I didn't see that charAt function that you mentioned. I'm surprised the text would ask me to do something in the problem when they didn't address it in the chapter. They show equals and compare to,
They show
if (name1.equals(name2))
       System.out.println ("These names are the same.");

I was trying to figure out if there was some way to use a statement like that to do the comparing. Maybe I missed the charAt.

Is there a way to compare the ascii codes, like with the <> = operators. How about using something like if (strText.(i) = 'a').

Using the charAt(i) would be the only way to do this?

#4
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
No its not the only way. You must understand that there is a difference between the concept 'string' and class String. When we are talking about the concept, we may think that its a sequence of characters, each has its own index, and we can look at what stands at some index. But once you get to writing code, we are talking about class String. And once we're talking about the class and its objects, we can not assume ANYTHING about its implementation or anything else that is not written in the interface. A string object is NOT an array! It is an object of the class String. The operator [] is not specified in the Java interface of String, that means you can't apply it on objects of class String. As simple as that. They do say though, that an object of String represents some sequence of characters. But because the implementation of this representation is not known, we must use methods called getters and setters to retrieve information about the objects state and change it, respectively.
Regarding another way of accessing the specific character - there is a method called toCharArray() in the String interface. It returns an object which is an array of characters that represent the appropriate string. You can call this function, and access the characters at the needed indexes of that array.

String (Java 2 Platform SE v1.4.2) - check the first method in method summary, right after the constructors part.
Oh and also don't expect to learn everything from books. The purpose of books (at least for learning a specific language) is to provide you with basic knowledge and tools of the language so you can learn on your own the rest. Remember, Google is your best friend after caffein.

#5
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Your right, I should have looked google but sometimes it's nice to be able to ask questions with what your struggling and sometimes I like to ask just for clarification. Your right though, I was just basing everything from what was in the text. I thought they covered everything I would need to know to be able to do the end of the chapter problems. I thought when I you mentioned. I assumed they wanted me to use the methods discussed and shown in the chapter to reinforce what I was reading about.

I knew the statement I was trying to create was wrong but I thought maybe there was a way to rearrange it to get it working. I understand now that it can't be done with the method I was showing.

Thanks for steering me in the right direction.

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
This may not get to you, but consider it for future reference...

Remember that in Java, there's no Operator Overloading, and Strings are objects and not intrinsic data types. The way you extract a character at a particular location in an object is to use the charAt method. To perform what you're trying to do with your loop, you should use charAt instead:
for (int i = 0; i < strText.length(); ++i)
{
    if (strText.charAt(i) == 'a' || strText.charAt(i) == 'A')
    {
        count_a++;
    }
}
I'd recommend using the for each syntax though, it's nicer to read, and unless you really need to worry about efficiency, perfectly fine:
for (char a : strText.toCharArray())
{
    if (a == 'a' || a == 'A')
    {
        count_a++;
    }
}

Wow I changed my sig!