Skip to content

Instantly share code, notes, and snippets.

@boyanov83
Created September 15, 2014 14:55
Show Gist options
  • Save boyanov83/8edf0c7fd563a19b65e2 to your computer and use it in GitHub Desktop.
Save boyanov83/8edf0c7fd563a19b65e2 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class _09_ListOfProducts
{
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
{
File text = new File("Input2.txt");
Scanner scan = new Scanner(text);
Map<Double,String> map = new HashMap<Double,String>();
String product = "";
Double price = 0.0;
while(scan.hasNextLine())
{
product = scan.next();
price = scan.nextDouble();
map.put(price,product);
}
scan.close();
TreeMap sortedMap = new TreeMap(map);
Iterator<Double> keySetIterator = sortedMap.keySet().iterator();
PrintWriter writer = new PrintWriter("Output2.txt","UTF-8");
while(keySetIterator.hasNext()){
Double key = keySetIterator.next();
writer.println(key + " " + sortedMap.get(key));
}
writer.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment