473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

New to java; String variable as a literal argument.

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.
Aug 18 '09 #1
2 3185
JosAH
11,448 Expert 8TB
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
Aug 18 '09 #2
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.
Aug 18 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
7
by: Christian Wilhelm | last post by:
Hi! I'm trying to call a Java WebService out of a .net Client. There are two Methods, one Method requires one Parameter of type Parameter, the other Method requires one Parameter of type...
7
by: Jorgen Haukland, Norway | last post by:
Hi, I have created a Java webservice which runs in IBM WebSphere appserver. I take the WSDL-file and create a VS.NET WinForm application and calls the service running on my PC and everything...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.