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: File Input- Multiple Sets of Data

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File Input- Multiple Sets of Data

    Hello, everyone! I posted this question at stackoverflow but they commented that they couldn't help me.
    Here is a link to my code.

    I am trying to create a Community Theater program that has Actors, Directors, Investors, Staff etc. I should eventually be able to input the information for the people from both a file and the keyboard. I'm working on trying to take in multiple Actors from one file and adding them to an ArrayList of Actors. My code takes in only one actor right now.

    I am trying to take in multiple sets of data for the same type of class but creating new instances after each set of data (16 lines). Each actor has 16 lines of data (text file included), so I was thinking that I would need perhaps a for loop but I am unsure of how and where to implement it. I am also unsure about adding these new actors to the Actor ArrayList. I'm a little shaky on the idea of creating multiple objects when it is unknown how many you need to create. Right now, I know how many Actors I need to add but it would be nice if I could learn how to expand that based on the user's needs.

    My Questions:
    • How can I write my program to know when it is done reading 16 lines and then initialize a new Actor (will expand to other people)?(expanded below)
    • Eventually, I plan on using my theaterObj (an instance of CommunityTheater class), what if I wanted to move the readActorFile method to that class? Would that change the way I implement anything majorly? Would it still be a static method? I ask this because I want the Actors array to be a variable in the CommunityTheater class. I would extend this to adding actors to an ArrayList in a Play class which would be in CommunityTheater as well. So, essentially a CommunityTheater has different Play(s), and each Play contains a list of Actors.


    I don't exactly know where I should put a loop (should I use a loop?) to read through each line, in readActorFile or in readInFile? How should I write this loop, as in, how do tell my program what line it's reading?

    What I've been thinking is that I would have a loop:

    for(countLine=0; countLine < 17; countLine++) or while(countLine<17)
       read all the info

    and then an if(?)-

    if(file has a next line)-I don't really know how to code this-
         start over with loop of reading information?

    I don't know about the for loop, as I'm not sure how to "count" lines in a text file.

    OR I've been thinking that readInFile in the Actor class could return something(true/false?) to let the method readInActor know that it's done reading, then readInActor can check to see if there's anything else in the file, if there is- it would call the readInFile in Actor class again to read in information... and so on. But again I am unsure of how to code this.

    Which would be a better way to go about this problem?
    Thanks if you read all of this, I know it's pretty long.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: File Input- Multiple Sets of Data

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    We typically won't follow links to code. Post correctly the code you need help with, preferably JUST the code you need help with.

    How much do you know about programming in Java? Is this your first project? First program? Do you have experience reading text files and manipulating or saving the data after it has been read?

    I suggest you take small steps. Create a text file with a few lines. Read the data in the few lines and store the data into objects. Once you have the text file (just a few lines) and code that reads the text file with the classes necessary to store the data, we have something we can help you with. If you need help getting started with that, try Google first, but come back with questions.

Similar Threads

  1. Nodes contain array data sets in a linked list. Need help iterating
    By Bawnawgwa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 10th, 2014, 06:11 PM
  2. open multiple text file to input to mysql
    By csharp100 in forum JDBC & Databases
    Replies: 2
    Last Post: November 26th, 2012, 09:58 PM
  3. Multiple User's Data
    By RLS0812 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 19th, 2012, 05:19 PM
  4. Multiple errors - Reading data from a file and using a GUI to display
    By toledospod in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 1st, 2011, 09:34 AM
  5. [ask]input data from java file to another java
    By bontet in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: October 29th, 2010, 10:50 AM