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:
I want to put John Doe into List, 23 into List, Mary Ann into List, etc. Thanks.
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?
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:
Name = new Choice();
....(Read file into array)...
int i = 0;
while (List[i] != null)
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.
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.
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?
In fact Person could provide a getFullname() method that returns firstname + " " + lastname, if need be.
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.
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.
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.