reading content of the text file, splitting it into strings
Hi, I'm totally new to java programming and struggling greatly with the project I set before me. Majority of what I have already done is digged out of the examples that can be found on the internet, so I usually will not be aware of easier way how to do what I want. Still, I try to understand the code I use, so explaining WILL help me greatly. I use Eclipse SDK (if that makes difference).
I have a problem with the import of data into a table. I have a text file that contains rows, each row dedicated to a single person, containing a name, surname, nickname, address and a phone number (5 items). These items are delimited by <tab>, while the data file contains several of these rows.
Now, I import these data using the
Code Java:
private void initData() {
String curDir = System.getProperty("user.dir"); //setting the working directory
File file = new File(curDir+"/players.dat"); //specifying the file name
try {
FileReader fr=new FileReader(file);
BufferedReader myInput =new BufferedReader(fr);
while(true)
{
String row; //created the string variable to hold the row data
row=myInput.readLine(); //read a line to the row variable
if(row==null) //if the row is empty, the procedure will end
{
break;
}
System.out.println(Arrays.asList(row.split(" "))); //goes through the non-empty row, splits the row based on the <tab> character and prints it to the console
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Until this moment, everything goes as expected. Now I would like to fill the table with the data, but fail at that. The rows are printed into the console as a whole, although the particular tab-delimited "words" are now separated by comas.
What I need (I think) is to fill some array type variable with these particular "words", and then call my (already prepared) class that would fill the items into the table row at the correct order. I have thought about something in the line with:
Code Java:
ManageRows newRow; //references to the class I have prepared for adding new rows and managing items in the row
for (int i = 0; i < 5; i++) {
????? //not sure what; should read the items on the given row one by one and insert them into the Array[i]
}
newRow.setName(Array[1]); // calls an object from the ManageRows class and writes the content of the Array[1] into it. The same goes with further objects
newRow.setSurname(Array[2]);
newRow.setNickname(Array[3]);
newRow.setAddress(Array[4]);
newRow.setPhone(Array[5]);
people.add(newRow); // adds the current row wit pre-filled Name, Surname, Nickname, Address and Phone values into the table
that would be in place of the System.out.println() method, but I couldn't find anything useful to use so far (instead of the question marks).
Can anybody help me with that?
Re: reading content of the text file, splitting it into strings
You already have the code that stores your data into a data structure.
Instead of printing out the List returned from Arrays.asList(), store it to a variable. Try printing out its size, iterating over each of its elements, etc.
Re: reading content of the text file, splitting it into strings
Quote:
Originally Posted by
KevinWorkman
You already have the code that stores your data into a data structure.
Instead of printing out the List returned from Arrays.asList(), store it to a variable. Try printing out its size, iterating over each of its elements, etc.
Thank you for the answer. My obvious problem is I do not know how to do that - haw to split the row to segments and fill the array with them. Any example of the needed code, please?
Re: reading content of the text file, splitting it into strings
Quote:
Originally Posted by
Dodo
Thank you for the answer. My obvious problem is I do not know how to do that - haw to split the row to segments and fill the array with them. Any example of the needed code, please?
You already have the needed code. You already split the rows (which returns an array), and you already convert that array into a List.
The only thing you have to do is, instead of simply printing it out and forgetting about it, save it to a variable and do whatever you want with it.
Re: reading content of the text file, splitting it into strings
OK, this kick helped somewhat, after an hour of try-and-error I have this part of the code for the issue:
Code Java:
private String[] textRow = new String[5];
...
ManageRows newRow = new ManageRows("Row");; //references to the class I have prepared for adding new rows and managing items in the row
for (int i = 0; i < 4; i++) {
textRow[i]=String.valueOf(Arrays.asList(row.split(" "))); //deleni slov podle tabelatoru
}
newRow.setName(textRow[0]);
newRow.setSurname(textRow[1]);
newRow.setNickname(textRow[2]);
newRow.setAddress(textRow[3]);
/*newRow.setPhone(Integer.parseInt(textRow[4]));*/
people.add(newRow); // adds the current row wit pre-filled Name, Surname, Nickname, Address and Phone values into the table
However, I'm still doing something wrong. As can be seen, a part of the code is commented out, because for some reason, every field of the table is filled by the data from the whole row, although separated by comas, not only by the particular part of the row. Any hint on how to overcome this? Any mistakes in the coding?
Re: reading content of the text file, splitting it into strings
Please understand what you're doing at each step.
Try playing with what is returned by row.split(" "). What type is it? What does it look like? Can you iterate over it? What is in each index?
When you know what that is, then figure out what the next line does: What does Arrays.asList() do with the value returned from row.split(" ")? Again, what type is it? What can you do with it?
Then go on to the next step: What does String.valueOf() do with the type returned from Arrays.asList()? The API is your friend here.
Re: reading content of the text file, splitting it into strings
Thanks a lot, I made it to work now! Now I'm going to toy with the thrown exceptions from entering the phone number (in some cases it is missing, thus the list is shorter and there is a non-existing array index). Once more, thank you for the help.
Re: reading content of the text file, splitting it into strings
Quote:
Originally Posted by
Dodo
Thanks a lot, I made it to work now! Now I'm going to toy with the thrown exceptions from entering the phone number (in some cases it is missing, thus the list is shorter and there is a non-existing array index). Once more, thank you for the help.
No problem. If you use a List instead of an array, you might be able to "get around" that problem by only adding valid numbers. But I'm not sure of your actual requirements, so it's up to you.