-
easy problem. i only know little java
I want to make a program that chooses 2 people at random. I want to have the program already know the people i am choosing. by a data file or something. And I want it to choose two people at random when i open it.
Thank you all so much,
thomas
-
you'll need I/O for this and the Math class method random(). in the file, maybe numbers could precede these persons names. if the number of persons in this file are predetermined already, then you need not worry about implementing a dynamic program. you just do something like a int person_A = (int)(Math.random * number_of_names_in_file ); do another to get another name. then your method that does the I/O reads the particular line with the resulting random numbers. if you keep on changing the number of names in the file, then maybe you can place a numer at the end or beginning of the file with the number of names in the list.
then again you can use an array or a vector to store all names in a file without prefexing them with numbers ( the former solution takes too much time and too much code). use a separator to store the names so that it won't be read as a single line or string. then you use the index of the array/vector and associate it with the resulting random number. i think this would be much better.
this is just a suggestion, i assume you know this much java. i hope you get my idea.
-
I'd suggest you read the names in fromt hte file using a StringTokenizer and put these names into a Vector and then use the method of random numbers above. The random number would just relate to the Vector number and you get the name from that number (as said above).
Don't forget to remember the first number and say that if the second number is equal to the first then roll a random number again
Meethoss
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks