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 8 of 8

Thread: Code to open up a txt file and display only first five lines?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Code to open up a txt file and display only first five lines?

    Im trying to write a java code for a homework problem in my intro java class but im having problems, frankly cause i dont know alot of loops and my text book doesnt help.

    This is the problem:
    Write a program that open the MyFile.txt and read from a file with these criteria.
    1)-The program should display the first five lines of the file’s contents only.
    2)-If the file contains less than five lines, it should display the file’s entire content. You must check for end of file and less than 6 lines on the loop iterations.
    3)- your program should use loop to read the line by line of the file MyFile.txt.
    The serperate myfile.txt has this

    I can still remember, at the end of that year, we sat for the last time in our circle together.
    She played us her favorite song, "like a bridge over troubled water"
    and quietly passed out a gift she had made for each of us.
    She had made each of us a pottery heart with our name and the date on it on the back each read
    "I believe in ME".We all cried including her.
    She told us she would always remember US. We believed she would.
    and this is what the code is suppose to look like after you run it.
    Java33.png


    And this is what i have so far:
    Java22.png


    Can someone show me what needs to go next to complete the java script to show the first 5 lines of the text files? Im really at a road block with this.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    i dont know alot of loops
    Take a look at the tutorial:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    If you have code that you have questions about, post it here.

    Be sure to wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    Thanks for the reply. Ive looked over the tutorials, while i see how some of them may help me put together the problem, im still confused on putting them where and how.
    import java.io.*;
    public class FileReader
    {
    	public static void main(String[] args) throws IOException
    	{
    		int counter=0;
    		String str;
    		FileReader freader = new FileReader("MyFile.txt");

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    The Scanner class's methods are easier to use for reading text files.

    You should NOT name your classes with the same name as classes in Java SE.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    darkr166 (April 7th, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    Ok i got closer with this with the help of google.

    import java.io.*;
     
    public class ReadTextFileExample {
        public static void main(String[] args) {
            File file = new File("MyFile.txt");
            StringBuilder contents = new StringBuilder();
            BufferedReader reader = null;
     
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
     
                while ((text = reader.readLine()) != null) {
                    contents.append(text)
                            .append(System.getProperty(
                                    "line.separator"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
     
            System.out.println(contents.toString());
        }
    }

    Ok it displays what i need it to display EXECPT it displays all 6 lines of the text files as suppose to just the first 5 which was a requirement for the problem. How do I display just the first five or leave out the last line?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    Use a variable to count the number of lines read and displayed. End the loop when the counter says 5 lines have been processed.

    Hint: the condition used by the while statement can have multiple parts:
    while(z < 2 && x !=23) {
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    darkr166 (April 7th, 2013)

  9. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    Ok i finally got it. Thanks!

  10. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code to open up a txt file and display only first five lines?

    How did you get it fix to show 5 lines only? pls help
    I am having an assignment exactly as this and do not know how to make it view 5lines. Please help! Thanks a lot

Similar Threads

  1. Reading lines from a txt file
    By neliJav in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2013, 01:54 PM
  2. How to skip and read lines in txt file
    By saran123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 26th, 2012, 08:07 AM
  3. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM