Advertisement
jbjares

randomSong

May 10th, 2015
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. public class RandomSongDataUtil {
  9.       public static void main(String[] args) throws FileNotFoundException {
  10.           System.out.println(getSoundSample()[4]);
  11.           System.out.println(getSoundSample()[5]);
  12.           }
  13.      
  14.       public static String[] getSoundSample(){
  15.           try{
  16.               String s = choose(new File("src/main/resources/itunessalesdata2.csv"));  
  17.               return s.split(";");           
  18.           }catch(Exception e){
  19.               throw new RuntimeException(e.getMessage(),e);
  20.           }
  21.       }
  22.  
  23.           public static String choose(File f) throws FileNotFoundException
  24.           {
  25.              String result = null;
  26.              Random rand = new Random();
  27.              int n = 0;
  28.              for(Scanner sc = new Scanner(f); sc.hasNext(); )
  29.              {
  30.                 ++n;
  31.                 String line = sc.nextLine();
  32.                 if(rand.nextInt(n) == 0)
  33.                    result = line;        
  34.              }
  35.  
  36.              return result;      
  37.           }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement