java - 在 Java 中使用 BufferedReader 处理换行符和空字符串

标签 java string

我正在学习Java。我相信我在理解 BufferedReader 如何处理“\n”或“”字符串(换行符和空字符串)时遇到问题。

如果我运行以下命令,如果我将这些字符串中的任何一个放入字符串数组中,它将失败。

    String [] strings = {"55", "23", ""};
    int total = 0;
    for (String str : strings)
    {
        if (str != null) {
            total += Integer.valueOf(str);
        }
    }
    System.out.println(total);

这很好,对我来说很有意义。对我来说没有意义的是当我在读取文件时运行此代码。

BufferedReader reader = null;
    int total = 0;
    try {
        reader = new BufferedReader(new FileReader("E:\\Testing\\Numbers.txt"));
        String line = null;
        while ((line = reader.readLine()) != null) {
                System.out.println(line);
                total += Integer.valueOf(line);
        System.out.println("Total: " + total);
    } catch(Exception e){
        System.out.println(e.getMessage());
    } finally {
        try {
            if (reader != null)
            reader.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

使用包含以下内容的文本文件:

5
2
3

运行没有错误。如果我在同一个文件 () 中添加一个空行,则会失败并显示消息 For input string: ""

我添加了 isNumeric函数来解决这个问题,但我不明白为什么当我运行没有任何空行的代码时 BufferedReader 会工作,即使它不喜欢“\n”或“”字符串。我在 javadocs 中查找了 valueOf(),但没有看到任何对我有帮助的内容。

这是我的最终代码,它使用 isNumeric 函数并显示它如何将“\n”和“”字符串视为非数字。

    BufferedReader reader = null;
    int total = 0;
    try {
        reader = new BufferedReader(new FileReader("E:\\Testing\\Numbers.txt"));
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (isNumeric(line))
            {
                System.out.println(line);
                total += Integer.valueOf(line);
           }
            System.out.println("Skipping a non numeric value");
        }
        System.out.println("Total: " + total);
    } catch(Exception e){
        System.out.println(e.getMessage());
    } finally {
        try {
            if (reader != null)
            reader.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

}
public static boolean isNumeric(String str)
{
    try
    {
        int d = Integer.parseInt(str);
    }
    catch(NumberFormatException nfe)
    {
        return false;
    }
    return true;
}


6
Skipping a non numeric value
1
Skipping a non numeric value
Skipping a non numeric value
2
Skipping a non numeric value
62
Skipping a non numeric value
23
Skipping a non numeric value
Total: 94

终于我看到了这个article在网站上,它很接近,但我仍然无法弄清楚。

最佳答案

当使用 BufferedReader 时,readLine() 方法将自动使用任何“新行”字符。

所以,本质上,您的初始文件是

5\n ...

在将字符串提供给代码之前,只需删除\n 即可。如果该行只是\n;然后你得到“”。一个简单的检查方法是 line.isEmpty()

关于:我不明白为什么当我运行没有任何空行的代码时 BufferedReader 会工作;好吧,我不明白这个问题。如果您的代码只读取带有数字的行,那么您在那里有可以处理空行的代码并不重要;或包含“无效”数字文本的行。

关于java - 在 Java 中使用 BufferedReader 处理换行符和空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944989/

相关文章:

java - JAXB 解码删除换行符

java - RSA公钥编码,Java和Android中,相同的代码,不同的结果

java - Struts 获取的返回字符串不正确。而不是 "input"它应该采取 "success"

C# 性能 - 正则表达式与多个拆分

python - 将字符串拆分为具有多个单词边界分隔符的单词

javascript - 不允许连续出现多个相同的字符

java - 将枚举作为 @WebParam 传递?

java - Spring Boot中的MySQL DataSource在其他Service/Control类中不可用并抛出空指针异常

java - 字符串获取行尾的文本

java - Android 出现奇怪的方框符号