easy problem. i only know little java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: easy problem. i only know little java

  1. #1
    Join Date
    Jan 2003
    Posts
    1

    Question 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

  2. #2
    Join Date
    Nov 2002
    Posts
    138
    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.

  3. #3
    Join Date
    Oct 2002
    Posts
    66
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center