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 7 of 7

Thread: Help printing from an array using outFile

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

    Default Help printing from an array using outFile

    Hi,

    I need to write from an array to an outfile. The code is below. My teacher says I need an additional loop somewhere, but I'm not sure where. Anything will help. Thanks!

    public static void saveToFile()       
        throws FileNotFoundException
      {
         PrintWriter outFile = new PrintWriter("theaterMovieData.txt");
     
         for(int i = 0; i < numTheaters; i++)
         {
           if(theaterObject[i] == null)
            break;
     
           Movie[] movieList = theaterObject[i].getMovies();
     
           outFile.println(theaterObject[i].getTheaterName() + " " + 
                           movieList[i].getMovieName() + " " + 
                           movielist[i].getMoviePrice()+ "0" + "END_OF_THEATER");     
         }
         outFile.println("END_OF_FILE");
         outFile.close();
      }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help printing from an array using outFile

    You have one loop iterating over the theaterObject array. What about the movieList array?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing from an array using outFile

    Thanks for responding Yes, that's the part the teacher added, but do I remove the the movieList array from the existing for loop and give it it's own loop? Where do I move the print command?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help printing from an array using outFile

    No you would need to nest the loops inside each other.
    for each theatre {
        get list of movies
        for each movie {
            do something
        }
    }
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing from an array using outFile

    Ok, I now have:
    public static void saveToFile()       
        throws FileNotFoundException
      {
     
          PrintWriter outFile = new PrintWriter("theaterMovieData.txt");
     
         for(int i = 0; i < numTheaters; i++)
         {
           if(theaterObject[i] == null)
            break;
            Movie[] movieList = theaterObject[i].getMovies();
     
            for (int j = 0; j < numTheaters; j++)
            {
              outFile.println(theaterObject[j].getTheaterName() + " " + 
                           movieList[i].getName() + " " + 
                           movieList[i].getPrice()+ "0" + "END_OF_THEATER");
              break;
            }
         }
         outFile.println("END_OF_FILE");
         outFile.close();
      }

    but it's only printing the first movie for the first theater and the second movie for the second theater.

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Help printing from an array using outFile

    Check your inner loop. For starters why is it looping numTheaters times? What if the numTheaters was 10 but the first theater only had 2 movies, it would try and loop 8 more times.
    Improving the world one idiot at a time!

  7. The Following User Says Thank You to Junky For This Useful Post:

    kcarls22 (November 21st, 2011)

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help printing from an array using outFile

    Here is the final working code:
    public static void saveToFile()       
        throws FileNotFoundException
      {
     
          PrintWriter outFile = new PrintWriter("theaterMovieData.txt");
     
         for(int i = 0; i < numTheaters; i++)
         {
           if(theaterObject[i] == null)
            break;
            Movie[] movieList = theaterObject[i].getMovies();
     
            outFile.print(theaterObject[i].getTheaterName()+ " ");
     
            for (int j = 0; j < movieList.length-1; j++)
            {
              if(movieList[j] == null)
                      break;
              outFile.print(movieList[j].getName() + " " + 
                           movieList[j].getPrice()+ " 0 ");
            }
            outFile.println("END_OF_THEATER");
         }
         outFile.println("END_OF_FILE");
         outFile.close();
      }
    Thanks for your help!

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 08:29 AM
  2. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 10:07 AM
  3. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 09:28 AM
  4. question about default of outFile()
    By odine in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: August 18th, 2010, 01:11 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 03:41 AM

Tags for this Thread