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: Difficulty adding to collection?

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

    Default Difficulty adding to collection?

    I think I have worded my title clumsily, as I am not 100% sure best way to identify my problem, so bear with me.


    I am trying to alter my code so that I can use a user supplied city id value to retrieve the City (an object) to update and then add the new YearData supplied by the user.

    As my code currently stands, I can create a new city, so when I output my file, I have the data for it.

    I can also add yeardata values (an arraylist of objects) which is also outputted to my file.

    However, at present, I cannot combine the two. I cannot create a new city AND populate the new city object with the yeardata Array. Does that make sense?

    City class:

    https://pastebin.com/G4XuivLt


    DataController class (has methods for adding data etc)


    https://pastebin.com/cTaas4st

    In particular,

     private void addYearData() {        
            System.out.format("\033[31m%s\033[0m%n", "Add Year Data");
            System.out.format("\033[31m%s\033[0m%n", "=============");  
            InputHelper inputHelper = new InputHelper();
     
             char c=inputHelper.readCharacter("Do you wish to add  new year data for a city? y to continue, t to terminate:");
            if (c=='y'||c=='Y')
                {
     
                String year=inputHelper.readString("Enter year:");
                 float precip=inputHelper.readFloat("enter precipitation value:");
                 int max=inputHelper.readInt("enter max temp");
                 int min=inputHelper.readInt("enter min temp");
                 int speed=inputHelper.readInt("enter wind speed");
                 String direction=inputHelper.readString("enter wind direction");
     
     
     
                    City newCity= new City(); 
                   YearData yd= new YearData(year, precip, max, min, speed, direction);   
     
                   char cz = inputHelper.readCharacter("Do you wish to add to an existing city?");
     
     
                   int id =inputHelper.readInt("Please enter the ID of the city you wish to add the yeardata to");
                           city.getId();
                           city.getyearDataCollection();
                   city.addYearData(yd);
     
     
     
               repository.add(city);
     
                }
            else if (c=='T'||c=='t')
                {
                    System.exit(0);
                }
     
        }

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Difficulty adding to collection?

    Please don't paste links to code. But please do the following:

    1. Create a small, complete and compilable program that demonstrates the problem. You can used smaller, dummy classes to keep it simple.
    2. Include any error message that you get when either compiling or executing the program.

    Regards,
    Jim

Similar Threads

  1. Replies: 1
    Last Post: November 28th, 2017, 05:17 AM
  2. Im having difficulty with this problem, can someone please help?
    By Pacifico13 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2014, 07:04 PM
  3. difficulty with this code
    By ch2207 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 4th, 2013, 08:21 AM
  4. JScrollPane Difficulty. Help Please
    By AceApple in forum AWT / Java Swing
    Replies: 2
    Last Post: June 24th, 2012, 05:30 PM
  5. [SOLVED] Difficulty with string passing...
    By Destined in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 05:43 AM