• Post Reply Bookmark Topic Watch Topic
  • New Topic

Save variables from a Randomly choosen line in a input file?  RSS feed

 
Paul Adkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I have made a program for school that reads text from a input file using scanner.
The input file it has 60 rows, each row is in the format: lastname, firstname, major, # credits
so the first 3 rows are like:

Stokes,Simon,IS,115
Haag,Timothy,HIST,93
Vaccaro,Rodney,BIOL,84

I have the program reading the file with a while loop, and then a series of IF statements that use a
.equals on a string variable for the students major, and then some if statements to determine which
class each student is in. Then the program is supposed to output in a JPanel, the number of students
in each class, in each of the 3 majors, IS,HIST,BIOL. I have it correctly doing this.

What I need to do now, is to use the random number generator to pick one of the first 25 students in
the text file, as a winner of a prize, and then just have some code displaying it like this in a new pop up
GUI box: [lname], [fname], of the [students major] department has won $1000.

I'm trying to figure out how to add some code to my whlie loop, so that as the file is being read, it stores
the lname, fname, major of the student that corresponds to a number (between 1-25), that I generedated
at the beginning of the program. All my code is a bit long, but I will attach it all if anyone has time to look
through it. But the main code that relates to this is:


I was going to post the whole code, but I can't seem to fine any file type that is accepted for the attachments. I may try to post
it in another message so people can see what I am talking about. Many thanks for any help or pointers.
 
Paul Adkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my whole code as of now, if anyone wants to look at it:
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Have you thought about using a Map to store courses so "HIST" can map to the History course, BIOL to the biology course, etc. Then you can have the values in each Course object.
 
Paul Adkins
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure what a Map is, is it a method or something? My java knowledge is still pretty basic, that big IF statement was the only way I could really think of to determine what class and major each studen was in.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case trySet up some Course objects with a recordCredits method, and use the recordCredits method to increment the number of people. Let the number of people be a field of the Course class. Provide methods to allow each course to return its code, how many students there are, etc.

Much better object-orientation like that.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul:

Here is the javadoc for the Map interface.

John.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!