Connecting Tech Pros Worldwide Forums | Help | Site Map

New to java; String variable as a literal argument.

Newbie
 
Join Date: Aug 2009
Posts: 2
#1: Aug 18 '09
Hello there!

I'm writing a 'hang man' program as a bit of a practice exercise and I've ran into a bit of a problem; I just need a little advise if anyone would be so kind.

My apologies if this has been covered here before; I've tried googling this but I don't know how to word it so my results have been quite unhelpful. I'll try my best to explain what I'm trying to do through example.

(Sorry if this is long winded :/)

I wrote a simple 2D shooting game not too long ago in which I used randomising images that also 'moved' randomly.
The way I randomised the images (possibly quite a dirty way of doing it?) was as follows:

I declared/defined a String, its value was the first half of an image path. I also created a String array which contained the second half of an image path.

Expand|Select|Wrap|Line Numbers
  1.     public String imagePath = "/finalpkg/";
  2.     public String myArray[] = {"img1.png", "img2.png", "img3.png", "img4.png"};
I then randomised the value that'd be returned for the 'second half' of the image path, combined it with the first half as a single string then used that string as the argument for 'setIcon'.
This was looped and used on multiple jLabels by the way.

Expand|Select|Wrap|Line Numbers
  1. nextImage = imagePath + myArray[myRand()];
  2.                 something();
  3.                 displayPhoto = new ImageIcon(getClass().getResource(nextImage));
  4.                 lblImage1.setIcon(displayPhoto);
Now back to what I'm currently doing...
I believe it's more or less the same principle.
I have 4 separate String arrays for different categories of words for my hang man game.
The user can select which category he/she wants to play with by a jComboBox.

Once the game has initiated the program saves the selected item from the jComboBox into a string. Each ComboBox item has the same name as the array it refers to.

Expand|Select|Wrap|Line Numbers
  1. chosenArray = (String)comboWord.getSelectedItem();
The arrays are in their own class, 'words'. What I've been trying to do is to 'add' "words." to the array name String to create "words."+chosenArray and have that String read literally, so that I can get the size of the chosen array and save it to an int variable in my 'game' class. (Then I can use that int as a range to randomly select a word from the String array.)

I've tried the code below but obviously that's horribly flawed. Is there a way I can do that to make the program read that newly created String literally? Or is there a better way of doing this?
(I don't want to learn bad practice)

Expand|Select|Wrap|Line Numbers
  1. game.arrayLength = ("words."+chosenArray).length;
Hopefully I explained that properly and it wasn't too confusing.
(Sorry, I'm new here. :P)

Thank you very much in advance for any help I may receive, It'd be most appreciated.

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 18 '09

re: New to java; String variable as a literal argument.


So basically the user picks a category and your program picks a word from that category. Suppose those categories are numbered 0, 1, 2, ... n-1 where you have 'n' categories. An array (or List) of categories can do the job and a selected category can give you a random word from that category.

kind regards,

Jos
Newbie
 
Join Date: Aug 2009
Posts: 2
#3: Aug 18 '09

re: New to java; String variable as a literal argument.


Ah yes, of course.
I'd been up for too long, I guess I was trying to make the program as clean as possible. (read: in as little lines as possible.)
Much better to keep it simple and readable.

That did the job.

Thank you very much.
Reply

Tags
arrays, length, strings