Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] select random filenames?

Started by cwurts Apr 28, 2004 at 8:05 PM 6 replies 500+ views
Original Post
cwurts
cwurts
Imagine that you were making a game where you used photographs of about 50 people with 5 photos of each person saved in gif format. In the game, only one person''s photos would be used, but that person would be selected at random. The obvious way to do this would be to use a massive select-case type statement using 50 different cases, but that is too long for me. Does anyone know of a way to select a filename by breaking it up into parts. For example, Bob''s photos are p01q1.gif, p01q2.gif ..... p01q5.gif and Mary''s photos are p02q1.gif.....p02q5.gif A random number(1-50) is selected and the name of the files used is determined by concatenation: first photo: filename1 = p + random(50) + q1.gif second photo: filename2 = p + random(50) + q2.gif fifth photo: filename3 = p + random(50) + q5.gif Does anyone know of a way to do this?
Corey Wurts
sirSolarius
sirSolarius
What''s wrong with what you just typed? Just make a string and add the numbers onto it... unless I don''t understand your question.
cwurts
cwurts
Objects of class File can be converted to Strings, but I need to convert a String to a File. Only Strings can be concatenated. That''s why the name of the file has to be a String first, then converted to a File. The problem is I don''t know of a way to do that.

Anybody else?
Corey Wurts
snyp
snyp
What are you talking about, why would you need it to convert to a filename??? IT IS ALREADY A FILENAME LOL!!! anyways why do you want random names, this isn''t for spam or viruses is it?
Actual Linux penguins were harmed in the making of this message.
cwurts
cwurts
Another possible way of doing it is to make a list of all the files in the directory and convert each one to a String and see if it matches the concatenated String. Does anybody know of a method called something like getnextFileInDirectory()?
Corey Wurts
Michalson
Michalson
Get out your standard library and use a template class resembling a list (vector for instance) to create a list of strings. Add all the file names to this list (you now have a list of 50 file names). Generate a random number from 1 to 50. Lookup the corresponding item in the list and use that filename.
Clu
Clu
quote:
Original post by Anonymous Poster
quote:
Original post by cwurts
Objects of class File can be converted to Strings, but I need to convert a String to a File. Only Strings can be concatenated. That''s why the name of the file has to be a String first, then converted to a File. The problem is I don''t know of a way to do that.


Maybe I don''t quite understand your question... But why can''t you do this:


//Create new random num generator seeded with current time

Random r = new Random(new Date().getTime());

//Generate a number between 1 and 50 inclusive

int photoNum = r.nextInt(50) + 1;

//Make filenames

String fileName1 = "p" + Integer.toString(photoNum) + "q1.gif";
String fileName2 = "p" + Integer.toString(photoNum) + "q2.gif";
String fileName3 = "p" + Integer.toString(photoNum) + "q3.gif";
String fileName4 = "p" + Integer.toString(photoNum) + "q4.gif";
String fileName5 = "p" + Integer.toString(photoNum) + "q5.gif";

//Make files

File f1 = new File(fileName1);
File f2 = new File(fileName2);
File f3 = new File(fileName3);
File f4 = new File(fileName4);
File f5 = new File(fileName5);


?

Any way, the Java API is your friend.

http://java.sun.com/j2se/1.4.2/docs/api/

Take a look at the File constructor, for instance. You''ll find out lots of cool new things.



You can even optimize out the calls to Integer.toString(). Java will do the right things when appending numeric types to strings automatically.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.