Trouble with strings and chars with arrays and loops

Started by
10 comments, last by TheChubu 10 years, 2 months ago

stringLength = x.length();
    String[] beautifulMind = new String[stringLength];
    String bMind = Arrays.toString(beautifulMind);
    bMind = bMind.toLowerCase();
    int[] charCount = new int[25];
    
    for (int i = 0; i < beautifulMind.length; i++)
    {
            if(bMind.charAt(i) = ("a"))
               {
                 charCount[0] = charCount[0]++;
                }
       

That's pretty much all the relevant code. I know bMind.charAt(i) isn't working because it needs a variable, at one point it was telling me invalid for a string and a char. I also had tried something similar to (bMind.charAt(i)).equals("a"); But that didn't work. I'm not sure exactly what would work here. If it isn't completely obvious. I want to take a string, and convert all of it to lower case, and then go through a loop to look at each letter in the sentence separately, and then add that to a count in an array, which I will later use to get statistics on all letters in the string.

Thanks.

Advertisement

charAt(i).equals('a');

perhaps? (note: single quotes to denote a char rather than a string)

(a char is not a string, although I'm n00b at Java but I do know C++ and C#)

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Thanks for the response! It tells me I cannot invoke equals(char) on the primitive type char when I tried that, but I really do appreciate the response!

If i change it to = ('a') it just tells me that the left side must be a variable.

charAt( int ) returns the primitive char, so you can't call functions on it. The mistake is obvious: you need to use == instead of =. Single equal sign assigns to variables (which is why your compiler is complaining about needing a variable), and double equal signs compare equality (which is what the if-statement is expecting).

Yo dawg, don't even trip.

charAt( int ) returns the primitive char, so you can't call functions on it. The mistake is obvious: you need to use == instead of =.

I honestly feel like I had tried that once, I probably tried it earlier, with a different, simple mistake, and so I never went back to ==... Thank you Boogy! If you know of a way I don't have to create an if statement for every single letter in the english alphabet, that would make this program a lot cleaner. Also, thank you again, because... it works now, lol!

Store the count as the value in some sort of associative container (a HashMap in Java I think), and increment the value each time you see a letter.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I myself would prefer to use a simple table method although it really depends I guess. A HashMap will do the job and will probably be more straightforward (I guess it depends on what you're more used to), but if you are bound to using arrays, the following solution is probably what I'd use:


final char alphabet[] = "abcdefghijklmnopqrstuvwxyz".toCharArray(); // A little ugly over here, but, that's meh.

String str = "Some string text goes here";
char charArr[] = str.toLowerCase().toCharArray();

int charCounters[] = new int[alphabet.length];
for(char character: charArr) {
   for(int i = 0; i < alphabet.length; i++) {
      if(character == alphabet[i]) {
         charCounters[i]++;
         break;
      }
   }
}

Yo dawg, don't even trip.

Yeah that is ok (not very efficient though, if the input is all zzzzzzzzzzzzzzzzzzzz) if you can guarantee that the input only has letters in the alphabet, otherwise, use the HashMap variant.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I've never used a hashmap before. I'd have to learn how to use one. His method seems okay, but like you said, not very efficient if the input is all z's or stuff. Thanks guys.

Yeah that is ok (not very efficient though, if the input is all zzzzzzzzzzzzzzzzzzzz) if you can guarantee that the input only has letters in the alphabet, otherwise, use the HashMap variant.

Of course, HashMap is the most reasonable choice as solution. I just have a special place in my heart for table methods ever since I discovered them :D

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement