Followers 0

# Trouble with strings and chars with arrays and loops

## 11 posts in this topic

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.

0

##### Share on other sites

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#)

1

##### Share on other sites

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.

Edited by ThinkingsHard
0

##### Share on other sites

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).

Edited by boogyman19946
1

##### Share on other sites

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!

0

##### Share on other sites

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.

2

##### Share on other sites

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;
}
}
}

Edited by boogyman19946
1

##### Share on other sites

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.

2

##### Share on other sites

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.

0

##### Share on other sites

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

0

##### Share on other sites
if(bMind.charAt(i) = ("a"))


There are several problems here: single = is an assignment operator. That is why you get a message that left side is not a variable. Change the single = to a double == to make a comparison.

Second: In most other languages, having double quotes makes a string (a pointer to a sequence of characters). Having single quotes makes a primitive char. Result: You are comparing a primitive char to a pointer. You should probably do well by changing to this:

if (bMind.charAt(i) == 'a')

Edit: I wrote a much more elaborate answer for you, but it got lost when I accidentally pressed tab and then backspace, causing my browser to go back to the previous page.  Yay, Chrome!  I always wondered why you have assigned backspace to "go back".  So done this a lot of times already...  Now I am kind of discouraged.  My point was very quickly explained elegant like this:

int characterCount[256];

//Remember to clear the array...

//Then you have some string "myString" or anything you name it

//Then the counting of occurrences is as simple as this:
for (int i = 0; i < length; i++) {
characterCount[myString.charAt(i)]++;
}

To print the statistics:

for (int i = 'a'; i <= 'z'; i++) {
std::cout << (char)i << " = " << characterCount[i];
}


Oh, it's Java!  Well, the idea is still there!  ;)  I am mostly into C++/ObjC for the time being, but just see this as pseudocode.  It's been years since I've touched Java, but I remember something like "System.out.writeLn" or something.

Edited by aregee
0

##### Share on other sites

Before recommending HashMaps, did no one wondered what are these three lines supposed to do?

1. String[] beautifulMind = new String[stringLength];
2. String bMind = Arrays.toString(beautifulMind);
3. bMind = bMind.toLowerCase();

First you create an array of strings then you convert the array to a single string, then you set it all to lower case. There is nothing there, you just created the array. Its an array filled with a bunch of nulls.

Just do System.out.printLn(bMind) and see what it prints. I think its not what you want.

I'm also not sure if you're differentiating what is a string and what is an array of characters here. A single string can be anything from nothing to a sentence to an entire book. An array of strings is an array of anything from nothing to sentences to entire books.
1

## Create an account

Register a new account