Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this code. Its working. By the way you should improve this code. The code is not optimised.</p> <pre><code>import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.TreeMap; public class Treemap { private List&lt;String&gt; fList = new ArrayList&lt;String&gt;(); private TreeMap&lt;Integer, List&lt;String&gt;&gt; tMap = new TreeMap&lt;Integer, List&lt;String&gt;&gt;(); public static void main(String[] args) { Treemap tm = new Treemap(); String file = ""; if (args.length == 1) { file = args[0]; try { tm.Read(file); } catch (IOException ex) { System.out.println("Error"); } } else { System.out.println("Usage: java treemap 'Filename'"); System.exit(1); } } public void Read(String file) throws IOException { //Scanner in = null; BufferedReader in = null; InputStream fis; try { fis = new FileInputStream(file); in = new BufferedReader(new InputStreamReader(fis)); String line = null; while ((line = in.readLine()) != null) { String[] file_Array = line.split(" ", 2); if (file_Array[0].equalsIgnoreCase("1")) { Add(Integer.parseInt(file_Array[0]), file_Array[1]); // Display(1); } else if (file_Array[0].equalsIgnoreCase("3")) { Add(Integer.parseInt(file_Array[0]), file_Array[1]); // Display(3); } else if (file_Array[0].equalsIgnoreCase("4")) { Add(Integer.parseInt(file_Array[0]), file_Array[1]); // Display(4); } } } catch (IOException ex) { System.out.println("Input file " + file + " not found"); System.exit(1); } finally { for(int i: tMap.keySet()) Display(i); in.close(); } } public void Add(int item, String fruit) { if (tMap.containsKey(item) == false) { fList = new ArrayList&lt;&gt;(); fList.add(fruit); tMap.put(item, fList); // System.out.println("Fruits added " + item); } else { tMap.get(item).add(fruit); // System.out.println("not exist"); } } public void Display(int item) { if (tMap.containsKey(item)) { System.out.print("Number " + item + ":" + "\n"); System.out.print(tMap.get(item)); System.out.print("\n"); } else { System.out.print(item + " WAS NOT FOUND"+ "\n"); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload