• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ThinkingsHard

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 this post


Link to post
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#)

Edited by Paradigm Shifter
1

Share this post


Link to 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
0

Share this post


Link to 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
1

Share this post


Link to 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!

0

Share this post


Link to 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.

2

Share this post


Link to 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
1

Share this post


Link to 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.

2

Share this post


Link to 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.

0

Share this post


Link to 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

0

Share this post


Link to post
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')

It should solve your problem.

 

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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0