Results 1 to 9 of 9
Thread: Reading a textfile
- 01-15-2010, 07:46 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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.
- 01-15-2010, 07:53 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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?
- 01-15-2010, 08:09 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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.
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:Java Code:Name = new Choice(); ....(Read file into array)... int i = 0; while (List[i][0] != null) { Name.add(List[i][0]); i++; }
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.
- 01-15-2010, 08:19 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 01-15-2010, 08:48 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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?
- 01-15-2010, 09:04 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
In fact Person could provide a getFullname() method that returns firstname + " " + lastname, if need be.
- 01-15-2010, 09:06 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
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.
- 01-15-2010, 09:15 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 40
- Rep Power
- 0
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.
- 01-15-2010, 09:18 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
-
grab the textfile
By Sticks_ll in forum New To JavaReplies: 3Last Post: 03-31-2009, 01:23 PM -
textfile into arrays.
By Sticks_ll in forum New To JavaReplies: 27Last Post: 03-16-2009, 08:00 AM -
Search TextFile
By gsupriyarao@yahoo.com in forum Advanced JavaReplies: 5Last Post: 04-11-2008, 11:03 AM -
reading textfile from java problem
By saytri in forum New To JavaReplies: 1Last Post: 01-17-2008, 02:13 AM -
Textfile and GUI problems
By saytri in forum New To JavaReplies: 2Last Post: 12-21-2007, 04:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks