Jump to content
• ### What is your GameDev Story?

• Advertisement

Public Group

# Trouble with strings and chars with arrays and loops

This topic is 1797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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.

#### Share this post

##### Share on other sites
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#)

Edited by Paradigm Shifter

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 13
• 9
• 15
• 14
• 46
• Advertisement
• ### Forum Statistics

• Total Topics
634059
• Total Posts
3015291
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!