Java - 导入文本文档

时间:2015-11-30 00:59:31

标签: java text

我有一个程序,它完美运行。我需要更改它的一部分以导入仅包含3个值的文本文档。文本文档如下: ” 0.35  0.75  .90“他们各自独立 - 叠在一起。

我试图在程序中用可从文本文档中读取这些值的变量替换这些实际的双精度数。当我尝试它时它不再有效。谁能指出我做错了什么?这是我的代码

import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class function {
static JFrame frame;


    //Function---showOutcome---------------------------------------------------------------------
    //This function will show the end results without showing all the background data.
    //-------------------------------------------------------------------------------------------
    static void showOutcome(int numFamily, double mean) throws FileNotFoundException{

        //
        //Define some variables and such
        //
        int totClaims = 0;
        double[] rn; //= new double[numFamily];
        int[] claims;  //= new int[numFamily];
        double[] distribution; //=new double[3]
        double[][] randomCValue; //=new double[numFamily][4];
        double[][] claimAmount; //new double[numFamily][4];
        double total = 0;
        double average = 0;


        claims = new int[numFamily]; //array that will hold the number of claims for each family
        distribution = new double[3];
        rn = new double[numFamily];  //this array will hold a random number that is used to calculate the number of claims per family
        randomCValue = new double[numFamily][4]; //this array will hold the random number to generate each claim
        claimAmount = new double[numFamily][4];//this array will hold the claim amounts of each claim

        //
        //Insert some tasks
        //

                //assign a random number to each family
                for(int i=0; i<numFamily;i++){
                rn[i]=(double)(Math.random());}

                //give each family a number of claims based on rn[i]
                String iRec;        // used to hold the input record from the "actual" file 
                Scanner iRecScan;   // scanner to scan for data in iRec

                File dData = new File("actual.txt");
                Scanner dDataScan = new Scanner(dData);
                for(int ii=0;ii<3;ii++){
                        iRec = dDataScan.nextLine();                    
                     iRecScan = new Scanner(iRec);
                    distribution[ii]=dDataScan.nextDouble();
                }

                for(int j=0; j< claims.length; j++){
                    if(rn[j]>distribution[2]){claims[j]=3;}
                    else if(rn[j]>distribution[1]){claims[j]=2;}
                    else if(rn[j]>distribution[0]){claims[j]=1;}
                    else if(rn[j]>=0){claims[j]=0;}
                    totClaims = totClaims+claims[j];}

1 个答案:

答案 0 :(得分:1)

当我运行您的代码时,我得到了Exception in thread "main" java.util.NoSuchElementException,这是由此部分代码引起的:

    for (int ii = 0; ii < 3; ii++) {
        iRec = dDataScan.nextLine();
        iRecScan = new Scanner(iRec);
        distribution[ii] = dDataScan.nextDouble();
    }

问题是,您阅读了文件的下一行,对其进行了一些处理,然后向Scanner询问nextDouble,如果您已达到结束文件,不存在

相反,你应该(可能)做更像......的事情。

    for (int ii = 0; ii < 3; ii++) {
        distribution[ii] = dDataScan.nextDouble();
    }