Results 1 to 9 of 9
  1. #1
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default Reading a textfile

    Is there a way to read this file:

    John Doe 23
    Mary Ann 27
    Mark Kay 33

    So that a variable reads John Doe as one String and not spilt the name up into John alone? The text file can't be changed into:

    John Doe
    23
    Mary Ann
    27
    Mark Kay
    33

    I want to put John Doe into List[0][0], 23 into List[0][1], Mary Ann into List[1][0], etc. Thanks.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why not create a Person(String firstName, String surname, int age(?)) class instead of using those N-dimensional lists.
    What is the purpose of reading the file?

  3. #3
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    One of the functions of my program is to take the names of the people in the file and create a list on an applet so I don't have to write .add("Random Name"); 2000+ times.

    Java Code:
    Name = new Choice();
    ....(Read file into array)...
    
    int i = 0;
    while (List[i][0] != null)
    {
        Name.add(List[i][0]);
        i++;
    }
    The code would end up something like this and then a list would show up on the applet with just the names. I already wrote the code to read this:

    John Doe
    23
    Mary Ann
    27
    Mark Kay
    33

    but I want to avoid each piece of information in the textfile to be on its on line. If I leave it as is, the text file will be over 14,000 lines long.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should not think about changing your data because of the way you want to display it.
    That's why a Person class is a good idea. You just load the data into a List<Person> and then views can get the parts of the data they want from getters on the Person class.

  5. #5
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    I'm not too familiar with Person(String firstName, String surname, int age(?)), but let's assume that it can read the info if it's in 1 line (like "John Doe 23"). Would it be able to read "John Mike Doe 23" also?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    In fact Person could provide a getFullname() method that returns firstname + " " + lastname, if need be.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Is the number always last?
    If so simply split, concatenate all bar the last bit (don't forget to add spaces) into the name, then the last bit is your number. Still ought to be a Person class, though. Possibly with firstname, lastname and a List<String> for middlenames.

  8. #8
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Would it be possible to do a if statement that checks if the next item on the file is a string or int and if its a string, concantenate them so both John Doe and John Mike Doe would both be treated as 1 string?

    Edit: The info per line in textfile is "John Doe 23 Outside 45 Inside 10 Over there 90". John Doe and Over there would be treated as 1 string each. If I can get the first example I posted to work, the rest should be easy.
    Last edited by Bomber_Will; 01-15-2010 at 09:17 AM.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Would it be possible for you to read the replies in this thread so far?
    Just read and split on space. Store the fragments into a Person class.
    If someone wants the name as one String concatenate the bits that they want together.
    That way you keep everything flexible while satisfying your requirements.

Similar Threads

  1. grab the textfile
    By Sticks_ll in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 01:23 PM
  2. textfile into arrays.
    By Sticks_ll in forum New To Java
    Replies: 27
    Last Post: 03-16-2009, 08:00 AM
  3. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 11:03 AM
  4. reading textfile from java problem
    By saytri in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:13 AM
  5. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 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
  •