#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Problem about inheritance


    I got a little problem , I need to store two new CSV file in my project.
    I had create two new class Fish and Dog,i had put some new methods and inheritance another class call pet into it, read those csv files in my FileIO,made a arraylist for them in a class control the model ApplicationModel, also i had
    give the right methods to the main methods and my viewer.

    There's no error in my project but it can't read my fish and dog csv file.
    I have this code for my pet :

    Code:
    public abstract class Pet {
       
            private String Shop;
            private String Type;
            private double Price;
            private String dateAcquired;
            private String notes;
    And new methods for my dog ( ignore fish for now)

    Code:
       public class Dog extends Pet {
        private String Size;
        private boolean neutered;
     
    
          public Dog(String csvString) {
            String[] attributes = csvString.split(",");
             this.setShop(attributes[0]);
             this.setType(attributes[1]);
             this.setPrice(Integer.parseInt(attributes[2]));
             this.setDateAcquired(attributes[3]);
             this.setNotes(attributes[4]);
             this.setSize(attributes[5]);
             this.setNeutered(attributes[6]);
       
     }       
    //getter setter methods for new stuffs
    
     @Override
    public String toString()
    {
        return this.getShop() + "\t" + this.getType() + "\t"
        +this.getPrice() + "\t" + this.getDateAcquired() +"\t"
        +this.getNotes()+this.getSize()+"\t"+ this.getNeuteredAsString()+"\n";
               
    }
    Everything works fine , but my fish and dog tab pages is still empty
    with no reason, no error, can someone please give me any tips?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    can't read my fish and dog csv file.
    Where is the code that you are having problems with? I don't see any code for reading files.
    The Scanner class has some easy to use methods that could be used to read a file.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    Where is the code that you are having problems with? I don't see any code for reading files.
    The Scanner class has some easy to use methods that could be used to read a file.
    i have these code to read files

    Code:
     public static ArrayList<Dog> readDogAsList(String csvFile){
              ArrayList<Dog> dog= new ArrayList<Dog>();
              BufferedReader infile = null;
              String fileName = "petshop-dogs.csv";
              String csvString;
              try
              {
                  infile = new BufferedReader(new FileReader(csvFile));
                  csvString = infile.readLine();
                  while(infile.ready())
                  {
                    dog.add(new Dog(infile.readLine()));
                  }
              }    
              catch(Exception d)
              {
               System.out.println("Failed to read dogs");;   
              }
              finally
              {
                if (infile !=null)
                {
                    try{
                        infile.close();
                    }
                    catch(Exception d)
                            {
               d.printStackTrace();   
              }
                }
              
                
              }
    
              return dog;
           }
           
           public static ArrayList<fish> readFishAsList(String csvFile){
              ArrayList<fish> fish= new ArrayList<fish>();
              BufferedReader infile = null;
              String fileName = "petshop-fish.csv";
              String csvString;
              try
              {
                  infile = new BufferedReader(new FileReader(csvFile));
                  csvString = infile.readLine();
                  while(infile.ready())
                  {
                    fish.add(new fish(infile.readLine()));
                  }
              }    
              catch(Exception f)
              {
               System.out.println("Failed to read fish");   
              }
              finally
              {
                if (infile !=null)
                {
                    try{
                        infile.close();
                    }
                    catch(Exception f)
                            {
               f.printStackTrace();   
              }
                }
              
                
              }
    
              return fish;
           }
           
           
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Does the code work like you want? If not please explain what is wrong with what the code does.

    If you get errors, copy the full text and paste it here.
  8. #5
  9. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,542
    Rep Power
    1621

    Problem inspection


    When you mentioned that your codes failed to read from the CSV files, have you printed out whatever returned by the readLine() method after the following line?
    java Code:
    csvString = infile.readLine();


    If there is an exception caught by your catch block, what was the exception thrown? If there is a stack trace produced, please post it here.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo