title: Java Adding new object to TreeSet and sorting it
body: <p>I'm having trouble figuring out some problems with a program that I'm trying to make. It's about taking information from a file, sorting the data and storing the new sorted data into another file. But because I'm still new to the Java, I'm having some trouble with a couple functions and methods. So here is what I've done so far:</p>
  
  <p>Public Class Planina:</p>
  
  <pre><code>public abstract class Planina {
  
     protected String mName;
     protected String Country;
      public Planina(String mname, String country) 
      { 
          mName = mname;
          Country = country;
      } 
      public Planina() 
      { 
          mName = "Name";
          Country = "Country";
      } 
      public void setmName(String mname) 
      { 
          mName = mname; 
      } 
      public String getmName() 
      { 
          return mName; 
      }
  
      public void setCountry(String country){
  
          Country = country;
      }
  
      public String getCountry(){
  
          return Country;
      }
      public String toString() 
      { 
          return "Mountain name: " + mName + "; Country: " + Country; 
      } 
  }
  </code></pre>
  
  <p>Public Class Vrah Extends Planina:</p>
  
  <pre><code>public class Vrah extends Planina implements Comparable&lt;Vrah&gt; {
  
  private String pName; 
  private int Height; 
  public Vrah(String mname, String country, String pname, int height) 
  { 
      super(mname, country); 
      pName=pname; 
      Height=height; 
      if(height&lt;0) 
          throw new IllegalArgumentException("Height cannot be negative!"); 
  } 
  public Vrah() 
  { 
      super("Name","Country"); 
      pName="Peak Name"; 
      Height=0; 
  } 
  public void setpName(String pname) 
  { 
      pName = pname; 
  } 
  public void setHeight(int height) 
  { 
      Height = height; 
      if(height&lt;0) 
          throw new IllegalArgumentException("Height cannot be negative!"); 
  } 
  public String getpName() 
  { 
      return pName; 
  } 
  public double getHeight() 
  { 
      return Height; 
  } 
  public String toString() 
  { 
      return super.toString() + "; Peak Name: " + pName + "; Height: " + Height; 
  } 
  @Override
  public int compareTo(Vrah o) 
  { 
      if(Height&lt;o.Height)return -1; 
      if(Height&gt;o.Height)return 1; 
      else return 0; 
  } 
  }
  </code></pre>
  
  <p>Chain Comparator:</p>
  
  <pre><code>import java.util.Arrays;
  import java.util.Comparator;
  import java.util.List;
  public class ChainComparator implements Comparator&lt;Vrah&gt; {
  
  private List&lt;Comparator&lt;Vrah&gt;&gt; listComparators;
  
  @SafeVarargs
  public ChainComparator(Comparator&lt;Vrah&gt;... comparators) {
      this.listComparators = Arrays.asList(comparators);
  }
  
  public int compare(Vrah vr1, Vrah vr2) {
      for (Comparator&lt;Vrah&gt; comparator : listComparators) {
          int result = comparator.compare(vr1, vr2);
          if (result != 0) {
              return result;
          }
      }
      return 0;
  }
  }
  </code></pre>
  
  <p>Height Comparator:</p>
  
  <pre><code> import java.util.Comparator;
   public class HeightComparator implements Comparator&lt;Vrah&gt; {
  
    @Override
      public int compare(Vrah vr1, Vrah vr2) {
          return (int) (vr1.getHeight() - vr2.getHeight());
      }
  }
  </code></pre>
  
  <p>Country Comparator:</p>
  
  <pre><code>import java.util.Comparator;
  public class CountryComparator implements Comparator&lt;Vrah&gt; {
  
  @Override
  public int compare(Vrah vr1, Vrah vr2) {
      return vr1.getCountry().compareTo(vr2.getCountry());
  }
  }
  </code></pre>
  
  <p>Public Class Varhove:</p>
  
  <pre><code>import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.RandomAccessFile;
  import java.util.Comparator;
  import java.util.Set;
  import java.util.TreeSet;
  
  
  public class Varhove{
  
  private Set&lt;Vrah&gt;vSet = new TreeSet&lt;Vrah&gt;();
  private String inFile = null;
  private String outFile = null;
  
   public Varhove(String infile, String outfile) throws IOException, FileNotFoundException {
       inFile = infile;
       outFile = outfile;
  
   @SuppressWarnings("resource")
   RandomAccessFile raf = new RandomAccessFile(inFile,"rw");
   String temp_data="";
  
   do {
       temp_data="";
       temp_data=raf.readLine();
  
       if(temp_data != null){
       String[] data = temp_data.split(" ");
       String mname = String.valueOf(data[0]);
       String country = String.valueOf(data[1]);
       String pname = String.valueOf(data[2]);
       int height = Integer.parseInt(data[3]);
       vSet.add(new Vrah(mname, country, pname, height));
       }   
       } while((temp_data != null) &amp;&amp; (temp_data.compareTo("") != 0));
       raf.close();
       }
  
   public void write() throws IOException,FileNotFoundException {
       if(outFile==null)
       return;
       File original = new File(outFile);
       original.delete();
  
       BufferedWriter out = new BufferedWriter(new FileWriter(outFile));
       for(Vrah it : vSet){
                  out.write(it.getmName() + " " + it.getCountry() + " " + it.getpName() + " " + it.getHeight());
                  out.newLine();
       }
       out.close();
       }
  
   public void printColl(){
   for(Vrah it : vSet)
   System.out.print(it.toString()+"\n");
   }
  }
  </code></pre>
  
  <p>Main Class:</p>
  
  <pre><code>import java.awt.List;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.Scanner;
  
  
  
  public class Main {
  
  public static void main(String[] args) {
  
      Varhove v_Coll;
      System.out.println("Enter one of the following commands:");
      System.out.println("1 - something..");
      System.out.println("2 - something else..");
      System.out.println("3 - exit");
      @SuppressWarnings("resource")
      Scanner scanchoice = new Scanner(System.in);
      System.out.println();
       int choiceentry = -1;
  
       do {
              System.out.println("Enter \"1\", \"2\" or \"3\"");
              choiceentry = scanchoice.nextInt();
  
              switch (choiceentry)
              {
                  case 1:
                       try {
                            v_Coll =  new Varhove("E:\\Programs\\Projects\\Java\\JavaProjectX\\inFile.txt", "E:\\Programs\\Projects\\Java\\JavaProjectX\\outFile.txt");
                            v_Coll.printColl();
                            v_Coll.write();
                           System.out.println("Data successfully saved.\n");
                       }
                       catch (FileNotFoundException e) {
                           e.printStackTrace();
                           } catch (IOException e) {
                           e.printStackTrace();
                           }
                      break;
                  case 2: 
                      Collections.sort(v_Coll, new ChainComparator(
                              new HeightComparator(),
                              new CountryComparator())
                      );
                      break;
                  case 3: 
                      // .. exit program
                      break;
                  default:
                      System.out.println("Choice must be a value between 1 and 3.");
              }   
          } while (choiceentry != 3);
  }
  }
  </code></pre>
  
  <p>So my first question is, how do I make a method that adds new object to the array, for example there is a choise in the menu that lets you add new mName,Country,pName and Height from the keyboard.</p>
  
  <p>And my second question is about an error giving on this line:</p>
  
  <pre><code>                case 2: 
                      Collections.sort(v_Coll, new ChainComparator(
                              new HeightComparator(),
                              new CountryComparator())
                      );
                      break;
  </code></pre>
  
  <p>I'm using ChainedComparator to sort the array in mulptiple fields but it seems like there is a problem about it: the method sort in the type collections is not applicable for the arguments. I know it's something about not implementing Comparable in the other classes, but is there a way to fix it and still use the ChainedComparator. Any help or example will be greatly appreciated. Thank you for your time.</p>