Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with TreeSet.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with TreeSet.

    My code:

     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package sortowaniekatalogow;
     
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.TreeSet;
    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    /**
     *
     * @author Rafał
     */
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
     
        public static void main(String[] args) {
     
            /* Obsługa argumentów wiersza polecen */
     
            Obiekt obj = null;
           if( args[0].equals("-read"))
           {
               if(args[2].equals("-sort"))
               {
     
                    obj= new Obiekt(args[1], true) ;
               }
               else
               {
                   obj= new Obiekt(args[1],false) ;
               }
                obj.wypiszOutput() ;
           }
           else if (args[0].equals("-write"))
           {
                if(args[2].equals("-sort"))
               {
                     obj= new Obiekt(args[1], true) ;
               }
               else
               {
                    obj= new Obiekt(args[1],false) ;
               }
     
                String filename = args[4] ; // scieżka do pliku gdzie ma zapisać
                    ObjectOutputStream save = null ;    // strumień obiektu
                    try
                    {
                        // otwieranie strumienia
                        save =  new ObjectOutputStream(new FileOutputStream(filename));
     
                        // zapis
                        save.writeObject(obj) ;
                    }
                    catch(IOException e)
                    {
                        // obsługa wyjątku IO
                        return ;
                    }
                    finally
                    {
                        try
                        {
                            // konieczne zamknięcie strumienia
                            save.close() ;
                        }
                        catch(IOException e)
                        {
                            // obsługa błędu zamknięcia strumienia
                        }
     
                    }
                    obj.wypiszOutput() ;    // wypisanie
                }
     
     
     
     
     
           else if (args[0].equals("-fread"))
           {
              String filename = args[1] ; // nazwa pliku gdzie zapisany jest obiekt
                ObjectInputStream load = null ;
                try
                {
                    // ładowanie danych przez strumień wejścia
                    load =  new ObjectInputStream(new FileInputStream(filename)) ;
                    try {
                        obj = (Obiekt) load.readObject();
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                catch(IOException e)
                {
                    // IO
                    return ;
                }
           }
     
    }
     
    }
     
    --------------------------
     
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package sortowaniekatalogow;
     
    import com.sun.org.apache.bcel.internal.generic.DDIV;
    import java.io.File;
    import java.io.Serializable;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    import java.util.TreeSet;
     
    /**
     *
     * @author Rafał
     */
    public class Obiekt implements Comparable<Obiekt>, Serializable{
     
        private String nazwa;
        private long modyfikowany;
        private long rozmiar;
        private Typ typ;
        private static SimpleDateFormat format;
        private Set <Obiekt> generyk ;
        public static int rec = 0;
        public File file;
     
        public static SimpleDateFormat getFormat() {
            return format;
        }
     
        public long getModyfikowany() {
            return modyfikowany;
        }
     
        public static void setFormat(SimpleDateFormat format) {
            Obiekt.format = format;
        }
     
        public void setModyfikowany(long modyfikowany) {
            this.modyfikowany = modyfikowany;
        }
     
        public void setNazwa(String nazwa) {
            this.nazwa = nazwa;
        }
     
        public void setRozmiar(long rozmiar) {
            this.rozmiar = rozmiar;
        }
     
        public String getNazwa() {
            return nazwa;
        }
     
        public long getRozmiar() {
            return rozmiar;
        }
     
        public int compareTo(Obiekt o) {
           return this.getNazwa().compareTo(o.getNazwa());
        }
     
        public void setTyp(Typ typ) {
            this.typ = typ;
        }
     
        public Typ getTyp() {
            return typ;
        }
     
        public Obiekt(String directory, boolean sort) {
            Obiekt obj;
            file = new File(directory);
            setModyfikowany(file.lastModified());
            setNazwa(file.getName());
            if(file.isFile()) // czy jest to plik
            {
                setRozmiar(file.length()); // dlugosc pliku
                setTyp(Typ.PLIK);  // typ
            }
            else if(file.isDirectory())
            {
     
                setRozmiar(file.list().length);
                setTyp(Typ.FOLDER);
     
                if(sort)
                {
     
                    generyk =  new TreeSet() ;
                }
                else
                {
                    generyk =  new HashSet() ;
                }
     
               File [] fileList = file.listFiles() ;
               for(int i = 0 ; i < getRozmiar() ; i++)
               {
                    obj = new Obiekt(fileList[i].getPath(), sort) ; // tworzenie obiektu
                    generyk.add(obj) ; // dodanie
                }
            }
        }
        public void wypiszOutput()
        {
            System.out.println(this) ;
            if(typ == Typ.FOLDER)
            {
                for(Obiekt plik : generyk)
                {
                    System.out.print("-") ;
                    for(int i = 0 ; i < rec ; i++)
                        System.out.print("-") ;
                    ++rec ;
                    plik.wypiszOutput() ;
                    --rec ;
                }
            }
     
        }
        @Override
        public boolean equals(Object obj) {
              if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
              return true;
        }
     
        @Override
        public int hashCode() {
            int hash = 12;
            hash = 31*hash + (this.nazwa != null ? this.nazwa.hashCode() : null);
            return hash;
        }
     
        @Override
        public String toString() {
           /* Formatowanie wyswietlania wynikow. */
     
            SimpleDateFormat formatDaty = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String output;
             if(typ == Typ.PLIK)
               return output = Typ.PLIK.toString() + '\n' + getNazwa() + '\n'+ formatDaty.format(getModyfikowany()) ;
            else
               return output = Typ.PLIK.toString() + '\n' + getNazwa() + '\n' + formatDaty.format(getModyfikowany()) ;
     
        }
     
     
         enum Typ {
        PLIK,
        FOLDER;
     
        }
    }

    It's something wrong with the tree.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem with TreeSet.

    That is a lot of code and most of the comments are not in English. You will have to describe the problem that is happening, because I have no idea what this is doing and what it should be doing. Simply saying there is something wrong with the tree will result in most of the credible helpers just passing over you since you were not able to provide any help regarding what the problem is.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] TreeSet to TreeMap Problem
    By MoniD in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 25th, 2011, 01:27 PM