Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: reading only certain lines from a .txt file

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reading only certain lines from a .txt file

    Hi i have this code that reads a this txt file below.

    My code:
    try {
        BufferedReader in = new BufferedReader(new FileReader("file.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            System.out.println(str);
        }
        in.close();
    } catch (IOException e) {
    }
    The File.txt:
    Ollie
    1
    2
    3
    ----
    Fred
    1
    2
    3
    ----
    I would like to edit the code so that it would be more like:
    str.equals("Ollie")
    System.out.println(1);
    System.out.println(2);
    System.out.println(3);

    i know thats not how you code it but just trying to show that it only reads the 3 lines below the name if the name equals "Ollie"

    Thanks in advance
    Ollie =]


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: reading only certain lines from a .txt file

    If there's any data you don't want printed out, don't. Just keep reading until you find what you want.

    BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
     
    while (reader.hasNext())
    {
         if (reader.nextLine().equals("Ollie"))
         {
              // prints out the three lines following the name Ollie
              System.out.println(reader.nextLine());
              System.out.println(reader.nextLine());
              System.out.println(reader.nextLine());
         }
    }

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading only certain lines from a .txt file

    hi thanks for the response, i tried using the reader.hasNext() but it just comes up with this error:
    cannot find symbol
    symbol: method hasNext()
    location: class java.io.BufferedReader
    i have imported:

    import java.io.*;
    import java.io.BufferedReader;
    import java.io.FileReader;

    but none of them seem to fix it, what is the problem?

    Thanks in advance
    Ollie =]

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: reading only certain lines from a .txt file

    Oops... my bad. I used the methods from the Scanner class

    note: you will need to either surround this code with a try/catch block, or add a throws declaration to your method.

    Scanner reader = new Scanner(new File("file.txt"));
     
    while (reader.hasNext())
    {
         if (reader.nextLine().equals("Ollie"))
         {
              // prints out the three lines following the name Ollie
              System.out.println(reader.nextLine());
              System.out.println(reader.nextLine());
              System.out.println(reader.nextLine());
         }
    }

  5. #5
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading only certain lines from a .txt file

    haha its ok just glad i have this it looks alot easier than what i have been doing!! XD

    yer thats what i have been doing for the BufferedReader aswell but thanks for the heads up!

    Thanks helloworld!! =]

    EDIT: for anyone else looking at this to make it work you need to:

    import java.util.Scanner;

    (add it at the very top of the code outside the class)
    Last edited by straw; March 7th, 2010 at 07:51 PM. Reason: Adding more info

Similar Threads

  1. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 03:40 AM
  2. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 01:56 AM
  3. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 09:14 AM
  4. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 10:31 AM
  5. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 02:07 PM