Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default How to read a 2d array file line by line

    My teacher hasn't showed us how to read a file like this

    Sample Data :
    7
    564 33 654 8321 15
    10 225 4557 254
    22 39 22 45 33 11 19
    1 2 3 4 5 6 7
    -10 9 1 8 -999
    -1 2 3 4 5 6 7
    0 34 45 23 12
    The program is supposed to read line by line and find the greatest number in the row.
    Can someone give me pointers on how to read one line at a time?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    The Scanner class has methods for reading from a file. You can read a whole line or you can read the file a token/word at a time.
    If you read a whole line at a time, you can use the Scanner class again with that line passed to its constructor to read each word on the line. Or you could use the String class's split() method to break the line into words and put them into an array.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    The Scanner class has methods for reading from a file. You can read a whole line or you can read the file a token/word at a time.
    If you read a whole line at a time, you can use the Scanner class again with that line passed to its constructor to read each word on the line. Or you could use the String class's split() method to break the line into words and put them into an array.
    Can I see an example please?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    The are dozens of code samples on this forum that use the Scanner class. Try doing a Search.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    The are dozens of code samples on this forum that use the Scanner class. Try doing a Search.
    I never learned about split() would a for loop do the trick?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Completely different functions.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Completely different functions.
    I'm confused with all the searches that pop up, none of them seem to relate to my question. Sorry for being a burden

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    what did you search for? What did you find?

    Also look in the API doc:
    http://docs.oracle.com/javase/6/docs/api/index.html

    Find the class in the lower left, click the link and the doc shows in the main frame
    Last edited by Norm; 04-18-2012 at 02:50 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    what did you search for? What did you find?

    Also look in the API doc:
    Java Platform SE 6

    Find the class in the lower left, click the link and the doc shows in the main frame
    I searched how to read a 2d array in a file

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    You might not find that. You will have to write your own code for that.
    You should look for how to use the classes I mentioned in post#2
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    You might not find that. You will have to write your own code for that.
    You should look for how to use the classes I mentioned in post#2
    Okay , I found the classes . But are there any examples out there?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Lots of examples. Do a Search here or use Google
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Lots of examples. Do a Search here or use Google
    So I could store the file contents into a 2d array using the split()? And then use 2 for loops to read it?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    The split() method creates a 1D array. You'd only need one loop.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    The split() method creates a 1D array. You'd only need one loop.
    Okay , I'm learning! So what delimiter would be used to separate each row?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    How is the data stored in the file? Normally with text files each line ends with a endline character. The nextLine() method handles that.

    What separates the tokens/words in each line/row read from the file? A space or comma or ?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    How is the data stored in the file? Normally with text files each line ends with a endline character. The nextLine() method handles that.

    What separates the tokens/words in each line/row read from the file? A space or comma or ?
    A space?

    Sample Data :
    7
    564 33 654 8321 15
    10 225 4557 254
    22 39 22 45 33 11 19
    1 2 3 4 5 6 7
    -10 9 1 8 -999
    -1 2 3 4 5 6 7
    0 34 45 23 12

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Looks like a space between tokens
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: How to read a 2d array file line by line

    Quote Originally Posted by Norm View Post
    Looks like a space between tokens
    How can I take one row of the file and make it into an array?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: How to read a 2d array file line by line

    Read it into a String and use split().
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 11-21-2011, 11:09 AM
  2. Replies: 16
    Last Post: 09-17-2011, 02:41 AM
  3. Line Reader changeing to read 1 line
    By Javanooby in forum New To Java
    Replies: 15
    Last Post: 05-10-2011, 04:34 AM
  4. Replies: 1
    Last Post: 02-13-2011, 03:09 PM
  5. Replies: 4
    Last Post: 09-10-2009, 10:18 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
  •