Post subject: Some java programming issues
Player (71)
Joined: 8/24/2004
Posts: 2562
Location: Sweden
Hi people, I'm once again poking around and playing with the java language. I never was a programmer, and I'll probably never will be. However I find it amusing from time to time to install a compiler and give it a go now and then. This time my issue is the following: I have a text file with string values in it. It actually contains numbers as well, but it is not for math purposes, so I consider them to be string values as well. I do not know how many rows this file contains (of course I do, but not for the purpose to work actually), and I need to import this file into a multidimensional array into the program. In the GUI that I've created I have check boxes which which should then output groups of what is actually contained in the file. Question is, how do I import the values in my file and then output only what I have selected in the GUI? If you want an example of the file it could look like this (making it separated with semicolon right now): Sun;Box1;10;Group1 Flower;Box2;20;Group2 Cloud;Box1;30;Group1 Sky;Box3;25;Group3 So for example, if I in my GUI select the group 1 check box, I only want my program to output those rows. If I select check box 2 it should only output group 2 from the file. Currently I have 12 groups, and these can be combined, so I could for example output Group 1, group 5, and group 12. Could you guys point me in the right direction? Delving through books and tons of pages on the internet is just not how it works for me. I've tried to read, and watched some tutorials, but I am much better off when shown straight away how it should be done. If it's not to much hassle it would be nice to see some code examples, and a great deal of good commenting. Cheers for any input.
Joined: 7/2/2007
Posts: 3960
What I'd recommend doing here is learning about string splitting. You have a delimiter, the ';' character, which (should) never show up in your actual strings. So you read in a line, and then split it on the ';' character. That gets you an array of strings. For example, splitting your first line there would get you ["Sun", "Box1", "10", "Group1"]. Then you can look at the last entry in that array to determine which group the line is in. You could also learn to use regular expressions for this. They're tremendously powerful string manipulators. However, I'd say they're overkill at this point. Also, have you considered that "1" would be entirely adequate instead of "Group1"? And you should probably start with 0 instead of 1 so you can use the number as an index into your array of groups -- just remember to check to make certain the number you read in is in-bounds.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Joined: 5/17/2008
Posts: 212
Location: Virginia
Try the StringTokenizer class. http://download.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html Read your text file in line by line, tokenize on your separator, act on the data. For storing the data, you could try using an ArrayList. http://download.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
adelikat wrote:
It started off fairly tame, but as more balls entered the picture it sure got a lot more entertaining.