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.

Page 2 of 2 FirstFirst 12
Results 26 to 48 of 48

Thread: Difficulty in creating/writing to a file (also, with reading file with data)

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    including a system.out.println statement in them doesn't generate anything
    If the code has a call to the println method and nothing is printed, that means the code is not being executed.
    Check to see if there is another version of the class that is being executed instead of the one that has the added println statements.

    I create a main method, attempt to invoke the method, can't do so
    Don't add any main methods. The debugging is supposed to be of the whole program when it is executed normally.
    The object of the print statements is to show the contents of items after something is added to it and when its contents are supposed to be displayed in the toString method.
    The current code shows that the City object being added to items is lost before the call to the toString method.

    Can you post the version of the Repository class that has the debug println statements?
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    package repositories;
     
    import java.util.ArrayList;
    import java.util.function.Predicate;
    import model.City;
     
     
    public class Repository implements RepositoryInterface {
        private ArrayList<City> items;    
     
        public Repository() {
            this.items = new ArrayList<>();       
        }
     
        public Repository(ArrayList <City> items) {        
            this.items = items;
        }
     
        public Repository(String filename) {
            this();
            // Create dao and execute load  
        }
     
        @Override
        public ArrayList<City> getItems() {        
            return this.items;
        }
     
        @Override
        public void setItems(ArrayList<City> items) {        
            this.items = items;
        }
     
        @Override
        public void add(City items) {
            this.items.add(items);
            System.out.println("value of items"+items);
        }
     
        @Override
        public void remove(int id) {
            Predicate<City> predicate = e->e.getId() == id;       
            this.items.removeIf(predicate);
        }
     
        @Override
        public City getItem(int id) {
     
            for (City item:this.items) {
                if (item.getId() == id)
                    return item;
            }
            return null;
        }
     
        @Override
        public String toString() {
     
            return "\nItems: " + this.items;
        }    
     
        @Override
        public void store(String filename) {       
            // create dao and execute store    
        }        
    }

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    That's half of it. Also copy that println statement in the add method to the toString method.
    Then compile the code and execute it to see what happens.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    package repositories;

    import java.util.ArrayList;
    import java.util.function.Predicate;
    import model.City;


    public class Repository implements RepositoryInterface {
    private ArrayList<City> items;

    public Repository() {
    this.items = new ArrayList<>();
    }

    public Repository(ArrayList <City> items) {
    this.items = items;
    }

    public Repository(String filename) {
    this();
    // Create dao and execute load
    }

    @Override
    public ArrayList<City> getItems() {
    return this.items;
    }

    @Override
    public void setItems(ArrayList<City> items) {
    this.items = items;
    }

    @Override
    public void add(City items) {
    this.items.add(items);
    System.out.println("value of items in the add method is:"+items);
    }

    @Override
    public void remove(int id) {
    Predicate<City> predicate = e->e.getId() == id;
    this.items.removeIf(predicate);
    }

    @Override
    public City getItem(int id) {

    for (City item:this.items) {
    if (item.getId() == id)
    return item;
    }
    return null;
    }
        @Override
        public String toString() {
               System.out.println("value of items in the toString method is"+items);
     
            return "\nItems: " + this.items;
     
        }    
     
        @Override
        public void store(String filename) {       
            // create dao and execute store    
        }        
    }

    I have to compile the whole project, because that file does not have a main method. The project runs, and I get:

    items []
    value of items in the toString method is[]
    Last edited by Tokugawa; November 19th, 2018 at 04:50 PM.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    there is absolutely nothing generated, the print statements dont run.
    Why? Do you mean nothing is printed? If nothing is printed, then the code is not executed. That could mean there is more than one version of the Repository class. One that you are making changes to and the other that is used when the program is executed.

    The idea of this debugging with the print statements in the Repository class was for the whole project to be executed normally to see what was printed when this following code from post#5 was executed:

                  repository.add(city);      //<<<<<< This should execute one of the println statements
     
                  System.out.println("value of repository is"+repository);   //<<<<<<<< this should execute the other println statement
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    I have to compile the whole project, because that file does not have a main method. The project runs, and I get:
    items [] 
    value of items in the toString method is[]

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    items []
    Where does that output come from? There isn't any posted code that shows that.


    Where is the output from the add method from this statement:
       System.out.println("value of items in the add method is:"+items);

    Where is the output from this line of code in post#5?
                   System.out.println("value of repository is"+repository);
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    System.out.println("value of repository is"+repository); that is no longer appearing, whether I load a file or not.

    System.out.println("value of items in the add method is:"+items); that appears in the console when I run the program
    Last edited by Tokugawa; November 19th, 2018 at 05:07 PM.

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    that is no longer appearing,
    Did you change the code in the load method in post#5 and remove that println statement?

    Can you restore all the code to what was in it when you made post#5?
    Then add the two println statements to the Repository class, compile and run the program so that it executes the load method.

    Can you post the current version of the load method?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package daos;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import model.City;
    import model.YearData;
     
    import repositories.Repository;
     
    /**
     *
     * @author mga
     */
    public class DAOTextImpl implements DAOInterface {
          private ArrayList<YearData>yeardata;
     
        static final char DELIMITER=',';   
     
     
     
     
        @Override
        public Repository load(String filename) {
     
            Repository repository = new Repository();
     
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) { 
     
                String[] temp;
                String line = br.readLine();
                while(line!=null){
     
     
                    temp=line.split(Character.toString(DELIMITER));        
                    int id = Integer.parseInt(temp[0]);
                    String cityName = stripQuotes(temp[1]);
                    String country=stripQuotes(temp[2]);
                    City city=new City (id, cityName, country);
     
       repository.add(city);
     
     
                      /*  line=br.readLine();
                        temp=line.split(Character.toString(DELIMITER));
                        String year=stripQuotes(temp[4]);
                        float precipitation=Float.parseFloat(temp[5]);
                        int maxTemp=Integer.parseInt(temp[6]);
                        int minTemp=Integer.parseInt(temp[7]);
                        int windSpeed=Integer.parseInt(temp[8]);
                        String windDirection=stripQuotes(temp[9]);
                     YearData yeardata=new YearData(year, precipitation, maxTemp, minTemp,windSpeed, windDirection); */
     
     
     
     
                     line = br.readLine();                
                }
     
     
     
     
     
     
     
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return repository;
     
        }
     @Override
     
     public void store(String filename, Repository repository) {
    try (PrintWriter output = new PrintWriter(filename)) {
    System.out.println("blah");
    System.out.println("value of repository is"+repository);
    System.out.println("value of repository is"+repository.toString());
    System.out.println("value of repository is"+repository.toString(DELIMITER));
    output.print(repository.toString());
     
    output.close();
     
    } catch (FileNotFoundException ex) {
    Logger.getLogger(DAOTextImpl.class.getName()).log( Level.SEVERE, null, ex);
    }
     
    } 
     
        private String stripQuotes(String str) {
            return str.substring(1, str.length()-1);
        }
    }

    No, not changed

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    What prints on the console when that version of the load method is executed?

    Add this statement after the call to add:
      System.out.println("value of repository is"+repository);
    Run the code and copy the contents of console and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    debug:
    Weather App
    ===========


    Load an existing File (Y/N)?:
    y
    Enter filename:
    tony2

    value of items in the toString method is!!!!!!!!!![]

    Items: []

    A. Add City B. Add new year data C. List Country Data In City Name Order D. List City Average Data E. List City Weather Data In City ID Order Q. Quit
    Enter choice:
    Q
    Enter filename:
    tony10
    Thank you for using Weather App. Good bye.

    BUILD SUCCESSFUL (total time: 15 seconds)

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    Where is the printout from the add method?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    Would it be easier if Just start from scratch?

    --- Update ---

    It doesn't output/appear

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    A problem is you are changing things and not telling me about it.
    There was supposed to be print statements in the add method and the toString method.
    The code in the load method was supposed to call the add method and the toString methods which would each print something.
    There wasn't a print out from the add method.

    I can't make any more suggestions because I have no idea what is currently in the code.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    There is a print statement in the add method, and the toString method. Only the toString method actually returned a value. The printout of the add method doesn't show/doesn't exist, as I've stated several times. I havent' changed anything without telling you :\

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    The printout of the add method doesn't show
    If there is a print statement in the Repository class's add method that does not print anything, then that method is not being called.
    If that method is not called, then it won't add to the items ArrayList.

    Where is the code for the add method that is called in the load method?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    Where is the code for the add method that is called in the load method?
    There isn't any.

    Do you mean the printout statement?

    That was in the repository class, not the class which has the load method :\

    Oh wait,

     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package daos;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import model.City;
    import model.YearData;
     
    import repositories.Repository;
     
    /**
     *
     * @author mga
     */
    public class DAOTextImpl implements DAOInterface {
          private ArrayList<YearData>yeardata;
     
        static final char DELIMITER=',';   
     
     
     
     
        @Override
        public Repository load(String filename) {
     
            Repository repository = new Repository();
     
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) { 
     
                String[] temp;
                String line = br.readLine();
                while(line!=null){
     
     
                    temp=line.split(Character.toString(DELIMITER));        
                    int id = Integer.parseInt(temp[0]);
                    String cityName = stripQuotes(temp[1]);
                    String country=stripQuotes(temp[2]);
                    City city=new City (id, cityName, country);
     
       repository.add(city);
     
     
                      /*  line=br.readLine();
                        temp=line.split(Character.toString(DELIMITER));
                        String year=stripQuotes(temp[4]);
                        float precipitation=Float.parseFloat(temp[5]);
                        int maxTemp=Integer.parseInt(temp[6]);
                        int minTemp=Integer.parseInt(temp[7]);
                        int windSpeed=Integer.parseInt(temp[8]);
                        String windDirection=stripQuotes(temp[9]);
                     YearData yeardata=new YearData(year, precipitation, maxTemp, minTemp,windSpeed, windDirection); */
     
     
     
     
                     line = br.readLine();                
                }
     
     
     
     
     
     
     
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return repository;
     
        }
     @Override
     
     public void store(String filename, Repository repository) {
    try (PrintWriter output = new PrintWriter(filename)) {
    System.out.println("blah");
    System.out.println("value of repository is"+repository);
    System.out.println("value of repository is"+repository.toString());
    System.out.println("value of repository is"+repository.toString(DELIMITER));
     
     
    output.print(repository.toString());
     
    output.close();
     
    } catch (FileNotFoundException ex) {
    Logger.getLogger(DAOTextImpl.class.getName()).log( Level.SEVERE, null, ex);
    }
     
    } 
     
        private String stripQuotes(String str) {
            return str.substring(1, str.length()-1);
        }
    }

    Did you mean:

    repository.add(city);

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    Did you mean:

    repository.add(city);
    Yes. The Repository class's add method has a print statement that should output the contents of items when it is called.
    Why didn't it print something? Where is the code for that add statement shown in the above quote?

    In the load method, where is this print statement:
                   System.out.println("value of repository is"+repository);
    It used to be right after the call to add. See post#5
    If you don't understand my answer, don't ignore it, ask a question.

  20. #45
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    Quote Originally Posted by Norm View Post
    Yes. The Repository class's add method has a print statement that should output the contents of items when it is called.
    Why didn't it print something? Where is the code for that add statement shown in the above quote?

    In the load method, where is this print statement:
                   System.out.println("value of repository is"+repository);
    It used to be right after the call to add. See post#5
    I dont know why I didnt print something.

    public class DAOTextImpl implements DAOInterface {
          private ArrayList<YearData>yeardata;
     
        static final char DELIMITER=',';   
     
     
     
     
        @Override
        public Repository load(String filename) {
     
            Repository repository = new Repository();
     
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) { 
     
                String[] temp;
                String line = br.readLine();
                while(line!=null){
     
     
                    temp=line.split(Character.toString(DELIMITER));        
                    int id = Integer.parseInt(temp[0]);
                    String cityName = stripQuotes(temp[1]);
                    String country=stripQuotes(temp[2]);
                    City city=new City (id, cityName, country);
     
       repository.add(city);

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    The last post truncated the end of the load method's code.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #47
    Junior Member
    Join Date
    Nov 2018
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package daos;
     
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import model.City;
    import model.YearData;
     
    import repositories.Repository;
     
    /**
     *
     * @author mga
     */
    public class DAOTextImpl implements DAOInterface {
          private ArrayList<YearData>yeardata;
     
        static final char DELIMITER=',';   
     
     
     
     
        @Override
        public Repository load(String filename) {
     
            Repository repository = new Repository();
     
            try (BufferedReader br = new BufferedReader(new FileReader(filename))) { 
     
                String[] temp;
                String line = br.readLine();
                while(line!=null){
     
     
                    temp=line.split(Character.toString(DELIMITER));        
                    int id = Integer.parseInt(temp[0]);
                    String cityName = stripQuotes(temp[1]);
                    String country=stripQuotes(temp[2]);
                    City city=new City (id, cityName, country);
     
       repository.add(city);
     
     
                      /*  line=br.readLine();
                        temp=line.split(Character.toString(DELIMITER));
                        String year=stripQuotes(temp[4]);
                        float precipitation=Float.parseFloat(temp[5]);
                        int maxTemp=Integer.parseInt(temp[6]);
                        int minTemp=Integer.parseInt(temp[7]);
                        int windSpeed=Integer.parseInt(temp[8]);
                        String windDirection=stripQuotes(temp[9]);
                     YearData yeardata=new YearData(year, precipitation, maxTemp, minTemp,windSpeed, windDirection); */
     
     
     
     
                     line = br.readLine();                
                }
     
     
     
     
     
     
     
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(DAOTextImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
            return repository;
     
        }
     @Override
     
     public void store(String filename, Repository repository) {
    try (PrintWriter output = new PrintWriter(filename)) {
    System.out.println("blah");
    System.out.println("value of repository is"+repository);
    System.out.println("value of repository is"+repository.toString());
    System.out.println("value of repository is"+repository.toString(DELIMITER));
     
     
    output.print(repository.toString());
     
    output.close();
     
    } catch (FileNotFoundException ex) {
    Logger.getLogger(DAOTextImpl.class.getName()).log( Level.SEVERE, null, ex);
    }
     
    } 
     
        private String stripQuotes(String str) {
            return str.substring(1, str.length()-1);
        }
    }

  23. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty in creating/writing to a file (also, with reading file with data)

    In the load method, where is this print statement:
                   System.out.println("value of repository is"+repository);
    It used to be right after the call to add. See post#5
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. File reading/writing
    By Masic1990 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 10th, 2014, 02:02 AM
  2. Reading and writing file
    By FaisalBahadur in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 12th, 2014, 05:38 PM
  3. Trouble with writing/reading array to/from file
    By MaximusPrime in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2012, 08:41 PM
  4. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  5. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM