Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: reading content of the text file, splitting it into strings

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: reading content of the text file, splitting it into strings

    Quote Originally Posted by KevinWorkman View Post
    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?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: reading content of the text file, splitting it into strings

    Quote Originally Posted by Dodo View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

    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?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Dodo (January 6th, 2011)

  8. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: reading content of the text file, splitting it into strings

    Quote Originally Posted by Dodo View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Reading from Text File and Processing Information
    By royalslim in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 13th, 2010, 03:27 PM
  2. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Reading from a text file. Help needed urgently.
    By TheAirPump in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 14th, 2009, 06:16 PM