如何检测扫描仪收到的BlankLine? [英] How can i detect BlankLine that Scanner has receive?

查看:59
本文介绍了如何检测扫描仪收到的BlankLine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取数据形式的文本文件,并且我使用扫描仪获取数据形式的文本文件. 这是配置文件保存模式

I want to get data form text files ,and I use Scanner to get data form text file. It's profile save pattern

name
status
friend
friend
.
.
(Blank line)

空白行在每个配置文件中都是分开的(朋友会一直循环直到下一行是空白行)

Blank Line is separate each profile.(friend will looping till next line is a Blank Line)

john 
happy
james

james
sad
john

然后我编写代码以获取文件格式文本

And i code to get file form text like this

try{
    Scanner fileIn = new Scanner(new FileReader("testread.txt"));
    while(fileIn.hasNextLine()){           
         String line = fileIn.nextLine();
         String linename = fileIn.nextLine();
         String statusline = fileIn.nextLine();
         println("name "+linename);
         println("status "+statusline);
         while(/*I asked at this*/)){
             String friendName = fileIn.nextLine();
             println("friend "+friendName); 
         }                              
    }
}catch(IOException e){
    println("Can't open file");
}

我应该使用什么条件来检测配置文件之间的空白行?

What condition that I should use to detect blank line between profile?

推荐答案

您可以实现以下自定义功能,如果该功能不为空,则会返回nextLine.

You can implement custom function like below which will return you nextLine if it is not empty.

 public static String skipEmptyLines(Scanner fileIn) {
    String line = "";
    while (fileIn.hasNext()) {
        if (!(line = fileIn.nextLine()).isEmpty()) {
            return line;
        }
    }
    return null;
}

这篇关于如何检测扫描仪收到的BlankLine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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