【问题标题】:finding numeric elements in an alphanumeric array read from a file and write them to a new file在从文件读取的字母数字数组中查找数字元素并将它们写入新文件
【发布时间】:2017-06-25 13:48:36
【问题描述】:

所以我的目标是读取一个文本文件,该文件是一个包含整数和字符串的数组,然后对一些数字进行一些计算,然后将原始数据与新计算的数据一起放入一个新的文本文件中。但是现在我被困在我似乎无法从文件中挑选出数字并将它们放入 ArrayList 的地方,除非我做错了。谢谢。

import java.util.*;
import java.io.*;


public class LAB02 {

  public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub  

    Scanner input = new Scanner(System.in);

    Scanner fileStockIn = new Scanner(new FileReader("Lab02Input.txt"));
    PrintWriter reportFile = new PrintWriter("Lab02Report.txt");



    ArrayList<Double> list = new ArrayList<Double>();

    while(fileStockIn.hasNext())    //while loop to stay in loop while there are more records
    {//begin while      
        double price = input.nextDouble();
        double shares = input.nextDouble();
        double dividend = input.nextDouble();
        String ticker = input.next();

         Double record = new Double(price, shares, dividend);
            list.add(record);


    }//end while


    fileStockIn.close( );
    reportFile.close( );        

  }

}

我正在读取的文本文件的输入(添加了要点以便于阅读):

  • 42.87 23.33 2.10 EXC
  • 12.00 83.33 0.17 亚视
  • 28.15 35.00 0.80 毫秒
  • 42.98 23.26 0.65 CVS
  • 33.64 29.72 2.20 TXN
  • 55.51 18.01 2.00 NVS
  • 16.00 62.50 0.40 SPLS
  • 19.81 50.47 0.24 CSCO
  • 30.09 33.23 1.76 T
  • 39.29 25.45 0.60 迪斯
  • 18.65 53.00 0.29 SNE
  • 50.21 19.21 0.72 AXP
  • 102.69 9.74 1.44 尼克

【问题讨论】:

  • 会发生什么?是否抛出异常?请提供更多信息
  • 你为什么把你的类命名为Double,这已经是一个Java类了?
  • 如果打算读取文本文件,您应该使用fileStockInScanner input = new Scanner(System.in); 在这里似乎没有用。
  • @TimBiegeleisen 将其命名为 double,因为没有其他任何东西在起作用,并且在网上的某个地方看到了类似的东西,所以我想试试。
  • 由于 java 是一种严格的语言,您应该能够使用预定义的函数来查找数组中的整数,然后将它们存储到一个临时数组中(对于十进制计算的双精度数),执行您的计算然后写入新文件。

标签: java file arraylist


【解决方案1】:

我不知道您如何尝试使用具有 3 个参数的构造函数来实例化 Double 类型。请参阅此处的文档,了解 Double 的工作原理。

https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html

您要做的是-您需要一个自定义类,它包含如下三个属性:

 private class Calculation  {
    int price;
    double shares;
    double dividend;

    //getters and setters here
    //constructor
    public Calculation (int price, double shares, double dividend) {
       this.price=price;
       this.shares = shares;
       this.dividend = dividend;
    }
   } 

然后在你的主课中

     List<Calculation> calculations = new ArrayList<Calculation>();
     while(fileStockIn.hasNext())      {
     //begin while      
        int price = input.nextInt();
        double shares = input.nextDouble();
        double dividend = input.nextDouble();
        String ticker = input.next();

        Calculation calculation = new Calculation(price, shares, dividend);
        calculations.add(calculation);
    }

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    为了简化它,只需使用如下内容:

    ArrayList<Int> ilist = new ArrayList<Int>();
    ArrayList<Double> dlist = new ArrayList<Double>();
    ArrayList<Double> dlist2 = new ArrayList<Double>();
    ArrayList<String> slist = new ArrayList<String>();
    
    ArrayList<Double> results = new ArrayList<Double>();
    
    while(fileStockIn.hasNext())      {
        ilist.add(input.nextInt());
        dlist.add(input.nextDouble());
        dlist2.add(input.nextDouble());
         slist.add(input.next());
    }
    

    现在您可以使用 .get(index) 和 for 循环来执行计算

    //just an example
    double total = 0;
    for(int i = 0; i < dlist.size() i++){
         total += dlist.get(i);
         results.add(dlist.get(i)/dlist2.get(i));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 2020-02-02
      • 2012-11-27
      • 2021-03-13
      • 2013-12-18
      • 1970-01-01
      相关资源
      最近更新 更多