【问题标题】:How to read this from file and get each value alone如何从文件中读取并单独获取每个值
【发布时间】:2017-11-13 13:09:15
【问题描述】:

我有这个文本文件,我需要将它提取到 Java 程序中以确定数组大小及其值。我可以单独读取每一行,但无法获取包含许多数字的行的值。另外,如何计算第三行中的元素。(0 2)

4
5
0 2

0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3

我正在使用的这段代码:

 List<Integer> list = new ArrayList<Integer>();
        File file = new File("D:\\ALGORTHIMS\\MASTER LEVEL\\dr. khaled\\assignment 1\\text.txt");
        BufferedReader reader = null;
         List<Double> ints = new ArrayList<Double>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                if (text.length() == 1) {
                    list.add(Integer.parseInt(text));
                } else {


                    String[] strs = text.trim().split("\\s+");


                    for (int i = 0; i < strs.length; i++) {
                        ints.add(Double.parseDouble(strs[i]));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {

            }
        }

//print out the list
        System.out.println(list);

        System.out.println(ints);

【问题讨论】:

  • Integer.parseInt(text) 将不起作用,因为文件中有 float 数字。请使用text.split("\\s+") 以空格分隔数字。
  • 它不起作用,检查我的更新

标签: java


【解决方案1】:

试试类似的东西

public static void main(String[] args) {
        List<Double> list = new ArrayList<>();
        File file = new File("text.txt");
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while ((text = reader.readLine()) != null) {
                String[] str = text.split(" ");
                for (int i = 0; i<str.length; i++) {
                    if(str[i].trim().length() > 0) {
                        list.add(Double.parseDouble(str[i]));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                System.out.print(e);
            }
        }

//print out the list
        System.out.println(list);
    }

【讨论】:

  • Double.parseDouble(str[i]) 是特定于平台的,不支持Locale。因此,您可能会在不同的笔记本电脑上遇到异常。
【解决方案2】:

看起来您正在使用与int 混合的双打,所以Integer.parseInt() 将不起作用。

你可以做的是将split这一行,用空格分隔成一个array,然后将每个元素放入list。请注意,这会将0 打印为0.0

while ((text = reader.readLine()) != null) {
      String[] numbers = text.split(" ");
      for (String number : numbers) {
           list.add(Double.valueOf(number));
      }
}

另外,您是否只是为您的代码复制了this 答案?

【讨论】:

    【解决方案3】:

    您可以使用java.util.scanner 相对轻松地从流中解析各种数据类型。

    例如,假设您要解析以下内容:

    1 2 3.0
    4.0 5 6
    

    您可以使用以下 sn-p:

    Scanner scan = new Scanner(System.in);
    int one = scan.nextInt();
    int two = scan.nextInt();
    double three = scan.nextDouble();
    // Defaultly, it will continue past the line break
    double four = scan.nextDouble();
    
    // We can also do this
    double five = scan.nextDouble();
    long six = scan.nextLong();
    

    一个重要的注意事项是java.util.Scanner 将抑制IOExceptions(尽管仍会引发转换文本的异常,例如NumberFormatExceptions)。你可以查看ioException 看看有没有被屏蔽。

    【讨论】:

      【解决方案4】:

      我认为使用Scanner 是最佳解决方案。 例如。如果我们有文件

      4 5
      
       0  1  2  3.1
       4  5  6  7.2
       8  9 10 11.3
      12 13 14 15.4
      16 17 18 19.5
      

      我们定义了一个 4 行 5 列的数组。然后要在int[][] arr 中获取它,我们可以使用以下方法:

      public static void main(String... args) throws FileNotFoundException {
          double[][] arr = null;
      
          try (Scanner scan = new Scanner(new FileInputStream(new File("text.txt")))) {
              scan.useLocale(Locale.US);  // to use '.' as separator
              int rows = scan.nextInt();
              int columns = scan.nextInt();
              scan.nextLine();
      
              arr = new double[rows][columns];
      
              for (int i = 0; i < rows; i++)
                  for (int j = 0; j < columns; j++)
                      arr[i][j] = scan.nextDouble();
          }
      
          System.out.println(Arrays.toString(arr));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        • 2020-03-16
        • 1970-01-01
        • 2013-11-11
        相关资源
        最近更新 更多