帮助:如何计算JAVA中的行数? [英] Help: How to Count number of lines in JAVA?

查看:70
本文介绍了帮助:如何计算JAVA中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好其他程序员,我有一个问题:编写一个静态方法,Display()接受表示文件名的字符串。该方法应该只读取并显示文件内容的前五行。如果文件包含少于5行,则应显示文件的全部内容。



(假设我已完成所有必要的导入)这是我的尝试:

 public static void Display(String filename)throws IOException 
{
File file = new File(filename);
Scanner read = new Scanner(file);

while(read.hasNext())
{
//到目前为止我已经尝试过这个
int count = 0;
String lineReader = read.nextLine();
count ++;

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

read.close();

}

解决方案

使用 java.util.Scanner 只是计算行数大矫枉过正!但是,你很亲密。声明一些整数计数器变量,在循环之前初始化为0,并在每次成功找到下一行时递增。



如果没有这样的过度杀伤,请使用 java.io.BufferedReader 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


  public   static   void 显示(< span class =code-sdkkeyword> String  filename) throws  IOException 
{

int lineCounter = 1;

文件file = new 文件(文件名);
扫描仪读取= 扫描仪(文件);

while (read.hasNext()&& lineCounter< = 5)
{

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

}

read.close();

}





[edit]已添加代码块[/ edit]


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();

}

解决方案

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


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]


这篇关于帮助:如何计算JAVA中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆