Results 1 to 6 of 6
  1. #1
    will_java is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default Reading file .txt

    Good morning everyone, I'm having the following problem:
    I'm trying to read data from a file qeu are organized as follows:

    2 0 0 0
    2 0 1 0
    2 0 2 0
    2 0 3 0
    2 0 4 0
    2 0 5 0
    2 0 6 0
    2 0 7 0
    2 0 8 0
    2 0 9 0
    time

    I just need to put each column into a vector, where only the first three columns, so I will have three vectors X, Y and Z. and stop to read when you see the word "time"

    I will then result in the vectors:

    X: 2 2 2 2 2 2 2 2 2 2
    Y: 0 0 0 0 0 0 0 0 0 0
    Z: 0 1 2 3 4 5 6 7 8 9

    Could someone send me a solution?

    Thank you.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    i would use only one vector/list. write a class which stores the three ints (x,y,z). use the scanner class to read the file, instantiate objects of that class, and store it in your vector/list.
    the scanner class has methods like hasNextInt and nextInt, next or nextLIne -->

    while(sc.hasNextInt()){ read the next three integer --> create an object --> save the object in your list}

    whats about the fourth integer/column? skip it ?
    Last edited by eRaaaa; 11-18-2010 at 05:27 PM.

  4. #4
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    Try this:

    File fileName = new File("C:\\Lovelesh\\Temp.txt");
    if(fileName.exists())
    {
    try {
    BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));

    String line = bufferedReader.readLine();
    Vector<String> a = new Vector<String>();
    Vector<String> b = new Vector<String>();
    Vector<String> c = new Vector<String>();

    while(null != line && (!line.equalsIgnoreCase("time") || line != null)){
    String[] temp = line.split(" ");
    if(temp.length > 1){
    a.add(temp[0]);
    b.add(temp[1]);
    c.add(temp[2]);
    }
    line = bufferedReader.readLine();
    }

    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    } catch (FileNotFoundException e) {

    e.printStackTrace();
    } catch (IOException e) {

    e.printStackTrace();
    }

    }

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

    Default

    @lovelesh: I wonder if your post isn't taking away the fun the OP might obtain from figuring out how to write the code. "Read the file line by line, split each line into its components and add each of the first three to three Vectors" would be a nudge that's gentle enough not to push the OP right out of the picture.

    But if you do post code, please use the code tags: put [CODE] at the start of the code and [/CODE] so that the resulting post is formatted and readable.

    ----------------------------------

    Java Code:
    //Vector<String> a = new Vector<String>();
        // why not
    List<String> a = new ArrayList<String>(); 
       // change ArrayList to whatever List implementation makes sense
       // given the specific requirements

    Basically there's no magic way to know that the Vector list implementation *is* the most appropriate (and a strong possibility that it isn't). And even it were the most appropriate, declaring the variable as "List<String> a = new Vector<String>()" would be more flexible.


    Java Code:
    //while(null != line && (!line.equalsIgnoreCase("time") || line != null)){
        // why not
    while(line != null && !line.equalsIgnoreCase("time")) {

    This latter pair aren't equivalent. As written your condition will fail to recognise the "time" line as a stopping condition.
    Last edited by pbrockway2; 11-19-2010 at 10:39 PM.

  6. #6
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    @lovelesh: I wonder if your post isn't taking away the fun the OP might obtain from figuring out how to write the code. "Read the file line by line, split each line into its components and add each of the first three to three Vectors" would be a nudge that's gentle enough not to push the OP right out of the picture.
    You are right, but this guy was looking for solution, that's why :-(

    Quote Originally Posted by pbrockway2 View Post
    But if you do post code, please use the code tags: put [CODE] at the start of the code and [/CODE] so that the resulting post is formatted and readable.
    I will keep this in mind, no on :-)
    ----------------------------------

    Quote Originally Posted by pbrockway2 View Post
    Java Code:
    //while(null != line && (!line.equalsIgnoreCase("time") || line != null)){
        // why not
    while(line != null && !line.equalsIgnoreCase("time")) {

    This latter pair aren't equivalent. As written your condition will fail to recognise the "time" line as a stopping condition.
    Thanks for this, I didn't review it before posting that ....my mistake
    Last edited by lovelesh; 11-20-2010 at 04:19 AM.

Similar Threads

  1. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:53 PM
  2. File Reading
    By nuggetman4 in forum New To Java
    Replies: 1
    Last Post: 05-10-2010, 05:49 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM

Posting Permissions

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