Results 1 to 5 of 5
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default iterate lines and characters per line

    hello Java Gurus,

    I would like to take the following text that is present in a file and perform operations on it, but I want to read the characters of the file, in sequence, like so:

    ***positions 1-3 for lines 1-3, then positions 4-6 for lines 1-3, etc, etc... so in other words:

    line1-char1, line1-char2, line1-char3,
    line2-char1, line2-char2, line2-char3,
    line3-char1, line3-char2, line3-char3,
    line1-char4, line1-char5, line1-char6,
    line2-char4, line2-char5, line2-char6,
    line3-char4, line3-char5, line3-char6,
    etc, etc...

    The file has many lines but the relevant lines are in blocks of 3 and each line spans 30 characters. Here's an example of a file:

    Java Code:
    |||  ||  | |
    |  ||| | | |
    ||  || ||
    does any one have suggestions on the easiest way to do this? using internal resources? are there any? I'm thinking only control structures will work for something like this. thanks for any help.

    (this is a dup. post from another forum)

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: iterate lines and characters per line

    First you have to get the relevant lines. This depends on how you are reading the file, but the approach would be read a line, decide whether it's "interesting" and if so process it, otherwise read the next line and repeat.

    As far as processing a line is concerned there is a String method that returns an array of char. This is just the thing to allow you process the line character by character.

    ---

    It's a good thing to post on both forums, links to the others. That way people responding can avoid wasting their time with something that's already been said, and see any other background etc.
    Last edited by pbrockway2; 03-18-2013 at 01:37 AM.

  3. #3
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: iterate lines and characters per line

    Quote Originally Posted by pbrockway2 View Post
    First you have to get the relevant lines. This depends on how you are reading the file, but the approach would be read a line, decide whether it's "interesting" and if so process it, otherwise read the next line and repeat.

    As far as processing a line is concerned there is a String method that returns an array of char. This is just the thing to allow you process the line character by character.

    ---

    It's a good thing to post on both forums, links to the others. That way people responding can avoid wasting their time with something that's already been said, and see any other background etc.
    so what exactly is that then? I don't need to know which line is interesting. I don't think I got what I wanted from this reply...

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: iterate lines and characters per line

    One method would be to read the file line-by-line and store the data in a Collection. Then you can go back and manipulate the data any way you like as many times as you like.

    Alternatively you can read each line, split the text as per your needs and place each segment into its own List. Example - read line one, get first three chars and place in List1, get second three chars and place in List2 etc.

  5. #5
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: iterate lines and characters per line

    Quote Originally Posted by Junky View Post
    One method would be to read the file line-by-line and store the data in a Collection. Then you can go back and manipulate the data any way you like as many times as you like.

    Alternatively you can read each line, split the text as per your needs and place each segment into its own List. Example - read line one, get first three chars and place in List1, get second three chars and place in List2 etc.
    Junky,

    what I'd like to do is find a way to code in reading 3x3 matrices from these lines. does that make sense? that way I don't have to patternize this stuff by issuing a million iterations. is there a matrix solution to this you think?

Similar Threads

  1. Replies: 1
    Last Post: 02-25-2013, 05:41 AM
  2. Replies: 2
    Last Post: 10-19-2011, 11:36 AM
  3. Two '\n' characters between lines while reading File
    By subith86 in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 10:56 AM
  4. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  5. Replies: 3
    Last Post: 12-17-2007, 03:35 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
  •