JAVA - 从文本文件中读取,识别新行

时间:2016-03-26 12:23:42

标签: java string char character

我有一个任务是读取包含多行的文本文件,之后我需要计算每个字符的UNICODE值,因此“hello”的总和是532,“你好吗”的总和是1059,依此类推字符串从.txt文档中的新行开始,到目前为止一直很好。 但是对于每一行,我只需要打印自己的值,以及我的代码的工作方式,它会增加每一行的值,当lxtine的结束时,我无法理解它的停止方式,所以它看起来像: *读行 *计算char值 *加起来 *打印它们 *重新开始下一行,等等

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.String;
import java.util.Arrays;

public class SumLines {

    public static void main(String[] args) {

        String filePath = "/home/lines.txt"; 
        String readLine;
        int sum = 0;

        try (BufferedReader bufferedReader = new BufferedReader(new    FileReader(filePath))) {
            while ((readLine = bufferedReader.readLine()) != null) {
                char[] array = new char[readLine.length()];
                System.out.println(readLine);

                for (int i = 0; i < readLine.length(); i++) {

                    Arrays.fill(array, readLine.trim().charAt(i));
                    sum += (int) array[i];

                    System.out.print(sum + " ");
                }
            }
        } catch (IOException e) {
            System.out.println("Error.\n Invalid or missing file.");
            e.printStackTrace();
        }
       System.out.println("\n*** final " + sum);
    }
}

4 个答案:

答案 0 :(得分:1)

如果我理解正确,输入:

hello
how are you

你想得到这样的东西作为输出:

hello 532
how are you 1059

*** final 1591

为此,您需要对代码进行一些修改:

  • 除了计算每行的字符总和值之外,还要保留所有行总数的另一个总和
  • 对于每个输入行,打印行后跟字符值之和
  • 您根本不需要阵列
  • 最好修剪输入行一次,而不是每个字符

像这样:

    int total = 0;

    try (BufferedReader bufferedReader = new BufferedReader(new    FileReader(filePath))) {
        String readLine;
        while ((readLine = bufferedReader.readLine()) != null) {
            String trimmed = readLine.trim();
            int sum = 0;
            for (int i = 0; i < trimmed.length(); i++) {
                sum += (int) trimmed.charAt(i);
            }
            System.out.println(readLine + " " + sum);
            total += sum;
        }
    } catch (IOException e) {
        System.out.println("Error.\n Invalid or missing file.");
        e.printStackTrace();
    }
    System.out.println("\n*** final " + total);

答案 1 :(得分:0)

for循环后,将sum设置为0.如果要打印总和,则需要另一个变量,例如t

像这样:

for (int i = 0; i < readLine.length(); i++) {
    Arrays.fill(array, readLine.trim().charAt(i));
    sum += (int) array[i];
    System.out.print(sum + " ");
}
t=t+sum;
sum=0;

然后在最后打印t

答案 2 :(得分:0)

一个简单的解决方案是限制sum变量的范围。这样,值不会在运行之间持续存在:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.String;
import java.util.Arrays;

public class SumLines {

    public static void main(String[] args) {

        String filePath = "/home/lines.txt"; 
        String readLine;
        int totalSum = 0;

        try (BufferedReader bufferedReader = new BufferedReader(new    FileReader(filePath))) {
            String readLine;
            while ((readLine = bufferedReader.readLine()) != null) {
                int sum = 0;

                for (int i = 0; i < readLine.length(); i++) {
                    sum += (int) readLine.charAt(i);
                }

                System.out.println(readLine + ": " + sum);
                totalSum += sum;
            }
        } catch (IOException e) {
            System.out.println("Error.\n Invalid or missing file.");
            e.printStackTrace();
        }
       System.out.println("\n*** final " + totalSum);
    }
}

此外,您不必使用这些复杂的东西来获取char的Unicode值。我做了一些改进。

答案 3 :(得分:0)

有两个变量,一个用于最终总和,一个用于行和。

  public class SumLines {

public static void main(String[] args) {

    String filePath = "/home/lines.txt"; 
    String readLine;
    int totalSum = 0;
    int lineSum = 0

    try (BufferedReader bufferedReader = new BufferedReader(new    FileReader(filePath))) {
        while ((readLine = bufferedReader.readLine()) != null) {
            char[] array = new char[readLine.length()];
            System.out.println(readLine);

            for (int i = 0; i < readLine.length(); i++) {

                Arrays.fill(array, readLine.trim().charAt(i));
                lineSum += (int) array[i];

                System.out.print(lineSum + " ");
            }
            totalSum += lineSum + totalSum;
            lineSum = 0;
        }
    } catch (IOException e) {
        System.out.println("Error.\n Invalid or missing file.");
        e.printStackTrace();
    }
   System.out.println("\n*** final " + totalSum);
}

}