8 Replies - 941 Views - Last Post: 01 November 2010 - 04:17 PM Rate Topic: -----

#1 bennitto   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Reading Files help

Posted 01 November 2010 - 02:27 PM

Just started self-learning reading files as it will be useful for an upcoming assignment.

The problem I am having is how to read values from a line in a text file, assigning each item on a line to a variable.

I am able to achieve this using a scanner object but I have been told to use the BufferReader class.

At the moment I know how to read a line from the text file and assign that to a variable.

Any help, examples and/or pointers on how to read individual items on a line to a variable will be appreciated.

This post has been edited by bennitto: 01 November 2010 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading Files help

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Reading Files help

Posted 01 November 2010 - 02:30 PM

Look, a tutorial!
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Reading Files help

Posted 01 November 2010 - 02:38 PM

View Postbennitto, on 01 November 2010 - 03:27 PM, said:

Just started self-learning reading files as it will be useful for an upcoming assignment.

The problem I am having is how to read values from a line in a text file, assigning each item on a line to a variable.

I am able to achieve this using a scanner object but I have been told to use the BufferReader class.

At the moment I know how to read a line from the text file and assign that to a variable.

Any help, examples and/or pointers on how to read individual items to a variable will be appreciated.

EDIT: a bonus will be how to calculate how many lines are in a text file.



The tutorial covers BufferedReader. Since you have to assign each line to a variable, I suggest using an arraylist to hold the lines and when you are done, take the size of the array and that is the number of lines in the array.
Was This Post Helpful? 0
  • +
  • -

#4 bennitto   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Re: Reading Files help

Posted 01 November 2010 - 02:45 PM

View PostJackOfAllTrades, on 01 November 2010 - 08:30 PM, said:




I had a look through the tutorial but it did not help me with what I wanted to achieve.

Btw, I figured out how to calculate the number of lines in a text file which did not use an arraylist.
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Reading Files help

Posted 01 November 2010 - 02:50 PM

View Postbennitto, on 01 November 2010 - 03:45 PM, said:

Btw, I figured out how to calculate the number of lines in a text file which did not use an arraylist.


So did I, but with an ArrayList, you kill more birds with one stone.
Was This Post Helpful? 0
  • +
  • -

#6 bennitto   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Re: Reading Files help

Posted 01 November 2010 - 02:57 PM

maybe I could have defined my problem a lot better but here's a second try.

Suppose each line of the text file held information like for example:

john doe 20


Instead of reading the whole line using BufferedReader, how would I read john, doe & 20 and assign them to 3 different variables?
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Reading Files help

Posted 01 November 2010 - 03:13 PM

You can use the String.split() method or a StringTokenizer.
Was This Post Helpful? 1
  • +
  • -

#8 bennitto   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Re: Reading Files help

Posted 01 November 2010 - 04:14 PM

thanks, I found the string split method very easy to use. I should point out that when I read through the StringTokenizer class, its use is discouraged
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Reading Files help

Posted 01 November 2010 - 04:17 PM

The StringTokenizer class more or less wraps around the split() method. This extra bulk of the additional code causes a StringTokenizer to take up more memory than the String[] from the split() method, which is why it is discouraged. That being said, if you find it more intuitive to use than split(), that's your call. StringTokenizer functions just fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1