public static void writePredictionsToFile(String testFile, String outputFileName, HashMap<Long, HashMap<Long, Double>> ratingPredictions) throws IOException{
		
	    FileWriter fileWriter = new FileWriter(outputFileName);
	    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
	    
	    Scanner scan = new Scanner(new File(testFile));
	    String line = null;
	    
	    boolean firstLine = true;
	    long currentUser = -1;
	    HashMap<Long, Double> currentUserPredictions = new HashMap<Long, Double>();
	    
	    //read and ignore first line (headers)
	    line = scan.nextLine();
	    
	    do {
	    	
	      line = scan.nextLine();
	      
	      String[] pair = line.split(",");
	      if(pair.length != 2){
	    	  System.err.println("Error: test file has wrong format. It must be a 2-column CSV.");
	    	  System.exit(1);
	      }
	      
	      long item = Long.parseLong(pair[0].trim());
	      long user = Long.parseLong(pair[1].trim());
	      
	      if(user != currentUser){
	    	  if(!ratingPredictions.containsKey(user)){
		    	  System.err.println("Error: test file contains user with uncomputed prediction.");
		    	  System.exit(1);
		      }
	    	  currentUserPredictions = ratingPredictions.get(user);
	      }
	      
	      if(!currentUserPredictions.containsKey(item)){
	    	  System.err.println("Error: test file contains item with uncomputed prediction.");
	    	  System.exit(1);
	      }
	    	  
	      double prediction = currentUserPredictions.get(item);
	      
	      if(!firstLine) bufferedWriter.newLine();
	      if(firstLine){firstLine=false;}
	      bufferedWriter.write(String.valueOf(prediction));

	    } while (scan.hasNext());
	    
	    bufferedWriter.close();
		
	}