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
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:
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?