www.webdeveloper.com
Results 1 to 10 of 10

Thread: Reading from a text file

  1. #1
    Join Date
    Aug 2004
    Posts
    61

    Reading from a text file

    whats up guys, got some more questions. we are starting to learn how to write and read strings to/from txt files. wel thats pretty easy, but how would i retrieve only the first 2 lines of the txt file, for example, if i wanted the first 2 lines, not any other, how would i do it:
    12 <--i only need this one and
    90 <--this one
    12
    6
    -9

    how would i go baout doin this.
    thnks

    -keko-

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    BufferedReader has a readLine() method that would be handy for that.

  3. #3
    Join Date
    Aug 2004
    Posts
    61
    umm, where would i find this method, cause i know sun has a page with all the methods availible to java. so maybe u can elaberate on what u said above or if u have a link to sun's java source page, cause i cant find it. thnks
    \
    -wes-

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715

  5. #5
    Join Date
    Aug 2004
    Posts
    61
    ok got the readline to kinda work. im reading from a txt file that contains 10 integers on 10 different lines. i need to read the first 2 lines, and get there some, thenm i need to read the next 2 lines. and add them up and etc..

    i got it to read from the file, but its not wokring right, it doesnt read the first line when i tell it to print and it only prints 5 lines? why is htis,
    /////////////////////////////////////////////////////////////////////
    this is the contenst of the file

    5
    5
    -10
    4
    0
    5
    6
    0
    8
    -4



    /////////////////////////////////////////////////////////////////////

    import java.io.*;
    import javax.swing.JOptionPane;


    public class wes{


    public static void main(String[] args)throws Exception{



    FileReader inStream = new FileReader("C:\\myText.txt");

    BufferedReader buffer = new BufferedReader(inStream);

    int numLines=Integer.parseInt(buffer.readLine()); //got thsi from an example, but it only reads the first line. which in the end messes upthe loop below? should i loop this to get a count?


    for(int lCount=0;lCount<numLines;lCount++){


    String data = buffer.readLine();
    System.out.println("de"+ data);
    }

    System.out.println("number of lines " + numLines );


    buffer.close();
    System.exit(0);




    }


    }

  6. #6
    Join Date
    Aug 2004
    Posts
    61
    ok, so dont pay no attention to the above reply. i believe thsi is the way to get everything done, everyhtign is the same as the print out my teacher gave us. the only thing is, how do i line everything up evenly. if u run this in JCreator or whatever u using, u will see how unaligned everything is, can someone help meo ut on how to align everyhting perfectly


    also, is this a correct way of writing thios program?
    -thnks-
    -wes-

    import java.io.*;
    import javax.swing.JOptionPane;


    public class wes{


    public static void main(String[] args)throws Exception{



    FileReader inStream = new FileReader("C:\\myText.txt");
    BufferedReader buffer = new BufferedReader(inStream);




    String data;
    data = buffer.readLine();
    int add = 0;
    int diff = 0;
    int prod = 0;

    System.out.println("Operand #1 " + " " + "Operand #2" + " " + " Sum " + " Difference" + " Product ");
    while(data!=null){


    int d1 = Integer.parseInt(data);


    data = buffer.readLine();

    int d2 = Integer.parseInt(data);
    add = d1 + d2;
    diff = d1 - d2;
    prod = d1 * d2;

    System.out.println(" " + d1 + " " + d2 + " " + add + " " + diff + " " + prod);

    data = buffer.readLine();

    }

    buffer.close();
    System.exit(0);

    }


    }

  7. #7
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    The reason its only printing the first five lines is because your storing the value of the first line in the numlines variable, which is 5.

    You could use a while loop instead.
    Code:
    String data = buffer.readLine();
    while (data != null)
    {
       ...
       data = buffer.readLine();
    }
    Regards.

  8. #8
    Join Date
    Aug 2004
    Posts
    61
    yea i did that. as soon as i postedthat i was having trouble, i went back and figured ito ut, thats hwy i wrote dont pay no attention to that reply, i did have a question again with new code if u wanna look at, scrool up, should be right above ur last post,

    thnks for the help

    -keko-

  9. #9
    Join Date
    Oct 2006
    Location
    USA
    Posts
    10
    JCreator has a feature which indents each line of a block of text if you highlight multiple lines and press Tab (Shift+Tab does the opposite).

    I did have a related question, but the program suddenly worked as I was typing this reply. For future reference, do you prefer a new thread for a similar problem?

  10. #10
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    If it's an unrelated problem, you may wish to make your own thread: you're likely to get more attention if you have asked your own separate queston. Good luck.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles