• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I print out a certain amount of lines from an ArrayList that was in another class  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been able to code a scanner that read in a file that I wanted it to and it added it to an ArrayList. I then tried calling the arraylist in the main class and had it print out into a new file and that worked. The only thing is, I am trying to get this file I read in to randomly generate only 1000 of the 10000 lines it read in.I will show you the code that I currently have that is working, and I will show you the changes I attempted to put in that are not compiling through.



 

In the main class I tried to change up the for loop to the following, but I got "no such exception" after I modified the ArrayList identifier, or I got a different compiler error when I kept the ArrayList identifier the way it is currently.

 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think: Is it legal to assign the kind of object that you're getting from that ArrayList to a String variable? If you declare an ArrayList<CityStateZip>, what kind of object will the ArrayList be holding?
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, random does not mean unique. With the way you're using random, there's a possibility of selecting the same element multiple times. I wouldn't think that's something you'd want to happen though. The alternative: shuffle all possible unique choices then use only the first 1000 of them. The real world analogy is those machines they use to choose lottery numbers where they have a bunch of balls, each with a different number on it, all randomly bouncing around. The winning lotto number is made up of the first six numbers taken out of the machine. On the other hand, Random does what Vegas slot machines do: they randomly spin three or four disks which may or may not all show the same figure when they stop spinning.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!