【问题标题】:how do I get data from a text file and save it into a string?如何从文本文件中获取数据并将其保存到字符串中?
【发布时间】:2017-06-10 06:26:55
【问题描述】:

如何从文本文件中获取数据并将其保存到字符串中?

例如,我的文本文件有数字 1、4、5、6、8 和 10.4。这些数字可以在同一行,也可以在不同的行。我想将它们连接成一个字符串,如下所示:1 4 5 6 8 10.4

import java.io.File;

import java.io.FileNotFoundException;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Scanner;

public class f {
    public static void main(String args[]) {
        int count = 0;
        double totalcount = 0;
        double average = 0;
        Scanner read = new Scanner(System.in);
        Scanner file;
        String input = "";
        String test = "";

        double[] array1 = new double[100];

        while (true) {
            System.out.println("Enter name of file or enter quit to exit");
            input = read.next();
            if (input.equalsIgnoreCase("quit")) {
                break;
            }

            try {
                file = new Scanner(new File(input));
                if (!file.hasNextLine()) {
                    System.out.println(input + " file is empty");
                }

                while (file.hasNext()) {
                    totalcount = totalcount + file.nextDouble();
                    count++;
                }
                while (file.hasNext()) {
                    test = test + (" ") + file.next();
                }

                System.out.println("bla" + test);

                average = totalcount / count;

                DecimalFormat df = new DecimalFormat("#.###");
                df.setRoundingMode(RoundingMode.CEILING);

                System.out.println("\nCount: " + count);
                System.out.println("Total: " + df.format(totalcount));
                System.out.println("Average: " + df.format(average));
                System.out.println();

            } catch (FileNotFoundException e) {
                System.out.println(input + " doesn't exist");
            }
        }
    }
}

我的代码不能正常工作。

【问题讨论】:

  • 听起来不错。到目前为止,此请求有任何错误吗?
  • 嗯,这就是我目前所拥有的,但它不起作用。 while (file.hasNext()) { test = test + (" ") + file.next(); }
  • 您能否在问题本身中显示minimal reproducible example?请edit
  • 我刚刚发布了到目前为止的内容,有点乱,我的目标是从用户那里获取文件名,然后将文件的内容保存到字符串中然后进行处理。
  • 你在第一个while (file.hasNext()) {之后用尽了整个扫描仪

标签: java java.util.scanner


【解决方案1】:

您的代码工作正常,问题是,您尝试访问文件的内容两次。在第一个 while loop 之后,hasnext() 方法将返回 false。因为您已经访问了第一个 @987654323 中的所有元素@ 环形。 所以它不会执行 -

                while (file.hasNext()) {
                test = test + (" ") + file.next();
                }

除此之外,您的代码还可以。 如果您还想将其存储在字符串中,请在您的第一个 while loop 中进行小修改,如下所示-

while (file.hasNext()) {
      Double d=file.nextDouble();
      test = test + (" ")+d;
      totalcount = totalcount + d;
      count++;
 }

我想这会给你想要的。

【讨论】:

    【解决方案2】:

    您好,我认为下面的代码对您有用,根据您的问题,我有带有数据的 txt 文件,首先我获取文件的位置,然后我尝试获取内容,最后我将其打印到控制台

    File f = new File("D:\\temp.txt");
    
        String content11 = FileUtils.readFileToString(f);
    
          System.out.println(content11);
    

    【讨论】:

    • 没那么简单。文件中的数字可以在单独的行中,也可以由任何类型的空格分隔。
    • 你试过我的代码了吗?如果是,请告诉我您得到的输出,如果不是,请尝试检查...
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多