Results 1 to 4 of 4
  1. #1
    Trad is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default Help with reading from file into an array

    Heyo!

    I am a student at Georgia State University and studying computer science and I have a question about a program I am writing for my data structures course. What I am currently trying to attempt is take a file and then read it and move all the words into an array. I am using a block of code provided to me by my instruction and I have a question about it.

    The code is:
    Java Code:
    BufferedReader input;
    StringTokenizer line;
    String inputLine;
    try {
    	input = new BufferedReader(new FileReader("Ages.dat"));
    	while ((inputLine = input.readLine()) != null) {
    		line = new StringTokenizer(inputLine);
    		// process line of data
    		...
    	}
    } // end try
    catch (IOException e) {
    	System.out.println(e);
    	System.exit(1); // I/O error, exit the program
    } // end catch
    I am assuming this is the correct code I need ... I am still learning concepts of programming and very new to Java.

    My question is this, when this block executes exactly what variable is the program sending the words from the file to? Just glancing at it I'm assuming they are all being saved to a string. Which variable is the data going to? I know this might sound elementary to some of you but I am having a time figuring it out and want to make sure I do this correctly!

    Also, after that what method is used to convert my string into an array? You could just point me to the method I need to use that is provided by the Java program and I can probably look it up in documentation and it take from there.

    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The BufferedReader, input which is using a FileReader reads a line in at a time with the line, input.readLine(). If this method returns null, then you've reached the end of the file. So with each loop of the while loop, the String variable "inputLine" will hold either a line from the file or null. You then are processing this String by feeding it into a StringTokenizer. What you're doing with this is not shown here.

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default StringTokenizer to array of Strings

    My question is this, when this block executes exactly what variable
    is the program sending the words from the file to?
    Your instructor's assignment is very complete.
    It delivers the files data to an object called
    a "StringTokenizer" named "line".

    You must look up the "StringTokenizer" in the
    Java API to find out what methods this object
    provides to process the line data into an
    array.

    It seems he wants word-strings minus
    whitespace entered into an array.

    For a beginning assignment, I hope his file
    has only one line, so that your process does
    not get too involved with array size.

    Anyway, from here you go nowhere until you
    investigate the "StringTokenizer" which
    will contain the line(s) from the file.

    Also, after that what method is used to convert my string into an array?
    Half of the algorithm for doing this lies in
    the methods contained in the StringTokenizer.

    Concern yourself with cleanly extracting a
    single word-string from a line first, then
    focus on getting it into an array of strings,
    then work on extending this process for the
    entire line within the StringTokenizer.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 01:51 AM
  2. Reading a txt file and then storing it in a 2d array
    By blkshp1990 in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 01:31 AM
  3. Reading text file into an array
    By Mahesh_ps in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 04:04 PM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 09:19 AM
  5. Reading input file into an array
    By littlefire in forum New To Java
    Replies: 6
    Last Post: 10-19-2008, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •