Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello there fellow programmers, I have a question: Write a static method, Display() that accepts a string representing the name of the file. The method should read and display only the first the five lines of the file's contents. If the file contains less than 5 lines, it should display the file's entire contents.

(Assuming that I made all the necessary imports) Here is my attempt:
public static void Display (String filename) throws IOException
{
   File file = new File (filename);
   Scanner read = new Scanner (file);

   while (read.hasNext())
   {
//So far I have tried this
       int count=0;
       String lineReader = read.nextLine();
       count++;

      if (count <5)
      {
        System.out.println(lineReader);
      }
   }
   
   read.close();

}
Posted
Updated 18-Apr-13 11:02am
v5
Comments
Sergey Alexandrovich Kryukov 18-Apr-13 16:25pm    
OK, and where is your question? :-)
—SA
fatand50 18-Apr-13 16:28pm    
It's on top before the code. But here it is:
Write a static method, Display() that accepts a string representing the name of the file. The method should read and display only the first the five lines of the file's contents. If the file contains less than 5 lines, it should display the file's entire contents.
Richard C Bishop 18-Apr-13 16:33pm    
That reads a lot like a homework assignment.
Sergey Alexandrovich Kryukov 18-Apr-13 16:35pm    
I answered the way not giving out all the "secrets". It's good to do by OP's own hands.
—SA
Richard C Bishop 18-Apr-13 16:38pm    
Nice work.

Using java.util.Scanner just for counting lines is a big overkill! However, you are close. Declare some integer counter variable, initialize to 0 before the loop and increment each time you successfully find next line.

Without such an overkill, use java.io.BufferedReader and java.io.BufferedReader.readLine:
http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html[^],
http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine%28%29[^].


—SA
 
Share this answer
 
v2
Comments
DinoRondelly 18-Apr-13 16:39pm    
+5, more then i would have given for this question, OP should learn something with this answer.
Sergey Alexandrovich Kryukov 18-Apr-13 16:44pm    
Thank you, Dino.
—SA
Espen Harlinn 18-Apr-13 17:01pm    
5'ed!
Sergey Alexandrovich Kryukov 18-Apr-13 17:12pm    
Thank you, Espen.
—SA
Java
public static void Display (String filename) throws IOException
{ 

   int lineCounter=1;

   File file = new File (filename);
   Scanner read = new Scanner (file);
 
   while (read.hasNext() && lineCounter<=5 )
   {

       String lineReader = read.nextLine();
        System.out.println(lineReader);
        lineCounter++;
      
   }
   
   read.close();
 
}


[edit]Code block added[/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900