View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Trouble with strings and chars with arrays and loops

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1ThinkingsHard  Members

Posted 14 February 2014 - 08:08 PM

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.

Posted 14 February 2014 - 08:10 PM

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, 14 February 2014 - 08:11 PM.

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

### #3ThinkingsHard  Members

Posted 14 February 2014 - 08:22 PM

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, 14 February 2014 - 08:23 PM.

### #4boogyman19946  Members

Posted 14 February 2014 - 08:24 PM

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, 14 February 2014 - 08:26 PM.

### #5ThinkingsHard  Members

Posted 14 February 2014 - 08:27 PM

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!

Posted 14 February 2014 - 08:31 PM

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

### #7boogyman19946  Members

Posted 14 February 2014 - 08:41 PM

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, 14 February 2014 - 08:44 PM.

Posted 14 February 2014 - 08:45 PM

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

### #9ThinkingsHard  Members

Posted 14 February 2014 - 08:52 PM

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.

### #10boogyman19946  Members

Posted 14 February 2014 - 10:04 PM

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

### #11aregee  Members

Posted 14 February 2014 - 10:59 PM

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, 14 February 2014 - 11:47 PM.

### #12TheChubu  Members

Posted 15 February 2014 - 12:06 AM

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.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.