Results 1 to 8 of 8
  1. #1
    shona is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default Reading textfiles

    :confused::confused:Hi I need help please! Im such a noob to java and Im having trouble with my assignment thats due in less than 48hours.
    1 of the problems is I keep getting an error when reading from a text file..

    Program details:
    Program is supposed to add actors to a filmshoot.When assigned to a filmshoot they will get a filmshootID. An actor can be added without actually being assigned to a filmshoot -so will not have a filmshootID. ..Ive never used this forum before..I hope this goes well as i have other problems

    This is the text file:
    101
    Brat Pitt
    m
    5
    102
    Mad Donna
    f
    58
    2
    103
    Britney Shears
    f
    41
    2
    104
    Tom Bruise
    m
    10
    105
    George Looney
    m
    78
    1
    106
    Jennifer Aniseed
    f
    17
    23
    107
    Jaymee Foxy
    m
    62
    2


    fields are ActorID, name, gender, age and the filmshoot ID that they have been assigned to. Bratt Pitt and Tom Bruise havent been assigned to a filmshoot so dont have filmshoot IDs... how does the program know when to read the next person's records if there is an inconsistency of fields as some have 4 and the rest have 5. like is there a loop needed to say "if an actor has a filmshoot ID - then read it otherwise move on to the next record?this is what i have so far.

    Java Code:
     private void readActorFile()
      {
        
        String fileName = PATH + ACTORFILE;
        
        // open text file
        try
        {
          BufferedReader filein = new BufferedReader(new FileReader(fileName));
          
          // read file into array
          String line = filein.readLine();
          int index = 0;
          while (line != null)
          {
            line = filein.readLine();
            int aId = Integer.parseInt(line);
            String aName = filein.readLine();
            String aGender = filein.readLine();
            line = filein.readLine();
            int aAge = Integer.parseInt(line);
            line = filein.readLine();
            int aFilmShootID = Integer.parseInt(line);
            line = filein.readLine();
            actors[index] = new Actor(aId,aName,aGender,aAge,aFilmShootID);
            numActors++;
            index++;
          }
          filein.close();
          System.out.println("Number of workers in system: " + numActors);
        }
        
        // catch any file opening errors
        catch(FileNotFoundException e)
        {  
          System.out.printf("Error!  File <filename> cannot be opened.\n", fileName);
          System.exit(0);
        }
        
        // catch any file reading errors
        catch(IOException e)
        {  
          System.out.printf("File <filename> cannot be opened.\n", fileName);
          System.exit(0);
        }
      }
    Moderator edit: code tags added

    okay sorry if I didnt explain my problem well:
    the 5 fields for the actor class are:
    int id
    String name
    String gender
    int age
    int filmShootID

    some actors dont have a filmShootID because they havent yet been assigned to a filmShoot. so their records in the textfile is as follows
    101
    name
    gender
    age
    whereas the next record might have
    102
    name
    gender
    age
    filmShootID
    the error I get when I run the program is: java.lang.NumberFormatException: For input string: "Brat Pitt"
    Ive tried googling it and asked friends heaps of times but cant find a good enough explanation and havent had much help.
    Thank you in advance for commenting and those who already have
    Last edited by shona; 05-30-2010 at 03:32 AM. Reason: give more info

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your post above to add code tags. In future posts, you should do this yourself, and the link in my signature below will tell you how to do this. Much luck!

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default

    Could you explain your problem a bit more? Are you getting errors or is it a logic problem: your program is not doing what you want it to.

    Do you have a definition for the format of the input file? What is in each record?
    an inconsistency of fields
    Where is this in the input file?

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Perhaps you need to use the pattern of line contents that is currently possible: numeric line, String, String, plus 1-2 numeric lines. Knowing this pattern it shouldn't be hard to test the lines for type of content using either regex or parse to int inside of try/catch and then use this information to tell where one record starts and the other begins.
    Last edited by curmudgeon; 05-29-2010 at 07:11 PM.

  5. #5
    ImAFycus is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Are you provided the text file or are you able to change the text file? The easiest fix would be to edit the text file before running you code and adding some symbol designate a missing field... this would be the least bug prone and most efficient code wise.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    First, this is a really bad design for the text file, but I'm sure that's on purpose, and part of the assignment. You sometimes have to cope with bad design imposed from outside in real life too.

    Let's walk this through in pseudocode, starting with the first record:
    Java Code:
            read int, assign to actorID
            read String, assign to actorName
            read String, assign to actorSex
            read int, assign to actorAge
    So far, so good. Now the next field is going to be an int, but you don't know whether that's the existing record's shootID, or the next record's actorID. And you won't know that until you read the record after that, and find out if it's an int (meaning our current int is shootID) or a String (which means our current int is the next actorID). So you need a way to look ahead. That means you want a Queue.

    Queue in Java is an interface, like Map, or Set, or List. The simplest implementation is LinkedList. So you set it up like this:
    Java Code:
    import java.util.*;
    
    public class ActorFileParser {
            private Queue<String> fields;
    
            public ActorFileParser() {
                    fields = new LinkedList<String>();
            }
            ...
    }
    Then the simplest thing would be to read the file and fill the Queue in one loop, then read from the Queue to build your records in a second loop. You use the add() method to put Strings at the tail of your Queue, and the remove() method to pull them off the head. You can also peek() at the head of the queue without removing it. So after you've read your actorAge field, you remove() the next int, then peek() at the field after that and see if it is an int. If it is, then the int you're holding onto is a shootID, otherwise it's the next actorID.

    This approach does mean that you are reading the entire file into memory, and building a large Queue. I doubt it's worth worrying about for your assignment, but if the file were huge (hundreds of megabytes) then it would be a concern. You would instead want to keep just two Strings at a time on the Queue, reading from the file as you go.

    -Gary-

  7. #7
    shona is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Hi ImAFycus
    We were given the actors records in the assignmet and I typed out the textfile myself.
    I tried replacing the non existing filmShootIDs with a 0 but that didnt work either. The work we practiced in class was so much different than this one. Every object had all its variables/records. The lecturer gave us some extra homework to do with this assignment

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default

    I tried replacing the non existing filmShootIDs with a 0 but that didnt work either
    Then all the records would have the same number of fields in the input file which wouldn't require the logic suggested by gcalvin.

    What "didn't work" with that input? Could you provide some more details?

Similar Threads

  1. Need help reading and writing with textfiles
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 02-08-2012, 07:25 AM
  2. reading from a .txt
    By Flamespewer in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 08:35 AM
  3. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  4. Reading from ObjectInputStream
    By deepthought015 in forum Networking
    Replies: 8
    Last Post: 04-28-2009, 05:57 PM
  5. Reading in strings
    By thekermo in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •