[prog] Java I/O problem 2

Finne Boonen hennar at gmail.com
Mon Apr 18 00:41:32 EST 2005


(there's prolly an easier way to do this, but still).

basically, take the string that reads the whole line, and look for
where the spaces are, keep track of these with indices, and take the
parts of the line in between the indices.

Finne

//////////////////////////////////////////////code///////////////////////////////////////////////////////////
 void readMyFile() {

    String record = null;
    int recCount = 0;

    try {

       FileReader fr     = new FileReader("test.txt");
       BufferedReader br = new BufferedReader(fr);

       record = new String();
       while ((record = br.readLine()) != null) {

        String item = "";

//the indices of where your spaces are, i1 is the left index of an item,
//i2 is the right index of one
//start with putting them on the first interesting places,
//the first place of the line for i1, and the first space in the line for i2
//indexOf will give you the index for the substring that's its argument
        int i2 = record.indexOf(" ");
        int i1 = 0;

//when there are no more occurences of the substring, the index will be -1,
//in that case we just want to go to the last case (slightly special)

        while (i2 != -1)
        {
                //take the item between the 2 indices
                item = record.substring(i1, i2);

                 //update the indices, move i1 to i2, and find the
next space for i2
                i1=i2;
                i2=record.indexOf( " ",i2+1);

                System.out.println(item);
         }

         //for the last index, just take the end of the line.
        item = record.substring(i1, record.length());

        System.out.println(item);

        System.out.println(record);

       }

    } catch (IOException e) {
       // catch possible io errors from readLine()
       System.out.println("Uh oh, got an IOException error!");
       e.printStackTrace();
    }

 } // end of readMyFile()

On 4/17/05, Noir <acknak_halflife at yahoo.co.uk> wrote:
> --- Noir <acknak_halflife at yahoo.co.uk> wrote:
> > I have a plain-text file and it reads
> > Tony 12.33 45.2 33.33 33.22
> > Jessica 11.3 33.3 12.33 45.5
> > Now, I have to read the file and give the output
> > like
> >
> > Tony
> >
> > 12.33
> > 12.33
> > 45.2
> > 33.33
> > 33.22
> >
> > Jessica
> > ...
> > ...
> >
> >
> > I can read the file and get the output (STDOUT). But
> > how am I going to break it down into new lines?
>
> My code looks like this:
>
> // FileReadTest.java
> // Copyright 1998 DevDaily Interactive, Inc.  All
> Rights Reserved.
>
> import java.io.*;
>
>   class FileReadTest {
>
> //--------------------------------------------------<
> main >--------//
>
>      public static void main (String[] args) {
>         FileReadTest t = new FileReadTest();
>         t.readMyFile();
>      }
>
>      //--------------------------------------------<
> readMyFile >--------//
>
>      void readMyFile() {
>
>         String record = null;
>         int recCount = 0;
>
>         try {
>
>            FileReader fr     = new FileReader("mydata.txt");
>            BufferedReader br = new BufferedReader(fr);
>
>            record = new String();
>            while ((record = br.readLine()) != null) {
>    //           recCount++;
>    //           System.out.println(recCount + ": " +
> record);
>                 System.out.println(record + "\n");
>            }
>
>         } catch (IOException e) {
>            // catch possible io errors from readLine()
>            System.out.println("Uh oh, got an
> IOException error!");
>            e.printStackTrace();
>         }
>
>      } // end of readMyFile()
>
>   } // end of class
>
> Send instant messages to your online friends http://uk.messenger.yahoo.com
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>

--
"Maybe you knew early on that your track went from point A to B, but
unlike you I wasn't given a map at birth!" Alyssa, "Chasing Amy"
http://hekla.rave.org/cookbook.html - my crossplatform dieet/recipe app

--
"Maybe you knew early on that your track went from point A to B, but
unlike you I wasn't given a map at birth!" Alyssa, "Chasing Amy"
http://hekla.rave.org/cookbook.html - my crossplatform dieet/recipe app


-- 
"Maybe you knew early on that your track went from point A to B, but
unlike you I wasn't given a map at birth!" Alyssa, "Chasing Amy"
http://hekla.rave.org/cookbook.html - my crossplatform dieet/recipe app


More information about the Programming mailing list