Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从CSV文件读取double_Java_Csv_Parsing_Save - Fatal编程技术网

Java 从CSV文件读取double

Java 从CSV文件读取double,java,csv,parsing,save,Java,Csv,Parsing,Save,我正试图写一个程序来计算中彩票的赔率/成本。以前的总花费和运行次数分别以双精度和整数形式保存到csv文件中。当我手工在文件中写入一个数字作为总花费(没有小数点)时,它运行良好,但一旦保存了该数字,我就无法再次运行它。我相信这个问题与用于较大数字的格式和/或使用parseInt有关,但我不确定。非常感谢您的帮助。谢谢 public class WinningTheLottery { public static final int SIZE = 5; public static void main

我正试图写一个程序来计算中彩票的赔率/成本。以前的总花费和运行次数分别以双精度和整数形式保存到csv文件中。当我手工在文件中写入一个数字作为总花费(没有小数点)时,它运行良好,但一旦保存了该数字,我就无法再次运行它。我相信这个问题与用于较大数字的格式和/或使用parseInt有关,但我不确定。非常感谢您的帮助。谢谢

public class WinningTheLottery
{
public static final int SIZE = 5;

public static void main(String[] args)
{
    int[] winningNums = new int[SIZE];
    int[] guessNums = new int[SIZE];
    int spent, runs = 1, oldRuns = 0, totalSpent = 0, bothRuns = 0;
    double oldTotal = 0.0, newAvg, bothTotal = 0.0;
    String line;
    String[] parts;
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    try
    {
        Scanner fileScanner = new Scanner(new File("average.csv"));
        while (fileScanner.hasNextLine())
        {
            line = fileScanner.nextLine();
            parts = line.split(",");
            oldTotal = Integer.parseInt(parts[0]);
            oldRuns = Integer.parseInt(parts[1]);

        }

        for (int i = 0; i < runs; i++)
        {
            spent = 0;
            randomlyAssignedNumbers(winningNums);
            // Arrays.toString(nameOfArray) => built in method to print an array
            System.out.println("\n[" + (i+1) + "] Todays winning numbers:\n" + Arrays.toString(winningNums).replace("[", "").replace("]", ""));
            do
            {
                randomlyAssignedNumbers(guessNums);
                spent++;
            } while (howManyCorrect(winningNums, guessNums) < 5);
            System.out.println("After spending " + currency.format(spent) + ", you won the Fantasy 5 lottery $75,000 prize!");
            totalSpent += spent;
        }
        bothTotal = totalSpent + oldTotal;
        bothRuns = runs + oldRuns;
        newAvg = (bothTotal/bothRuns);
        PrintWriter fileWriter = new PrintWriter(new File("average.csv"));
        fileWriter.println(bothTotal + "," + bothRuns);
        System.out.println("\nAverage Cost to win the Lottery: " + currency.format(newAvg));
        fileScanner.close();
        fileWriter.close();
    }
    catch(IOException e)
    {
        System.out.println(e.getMessage());
    }

}

public static void randomlyAssignedNumbers(int[] anyArray)
{
    Random rng = new Random();
    for (int i = 0; i < anyArray.length; i++)
    {
        anyArray[i] = rng.nextInt(36) + 1;
    }
}

public static int howManyCorrect(int[] a1, int[] a2)
{
    if (a1.length != a2.length)
        return -1;
    int count = 0;
    for (int i = 0; i < a1.length; i++)
    {
        if (a1[i] == a2[i])
            count++;
    }
    return count;
}
}
公共类WinningTheLottery
{
公共静态最终整数大小=5;
公共静态void main(字符串[]args)
{
int[]winningNums=新的int[大小];
int[]guessNums=新的int[SIZE];
int-spend,runs=1,oldsruns=0,totalspend=0,bothRuns=0;
双倍oldTotal=0.0,新平均值,bothTotal=0.0;
弦线;
字符串[]部分;
NumberFormat currency=NumberFormat.getCurrencyInstance();
尝试
{
Scanner fileScanner=新扫描仪(新文件(“average.csv”);
while(fileScanner.hasNextLine())
{
line=fileScanner.nextLine();
零件=直线分割(“,”);
oldTotal=Integer.parseInt(部分[0]);
oldrunts=Integer.parseInt(部分[1]);
}
for(int i=0;i打印数组的内置方法
System.out.println(“\n[”+(i+1)+“]今天的中奖号码:\n“+数组.toString(winningNums).replace(“[”,”).replace(“]”,”);
做
{
随机分配数(猜测数);
用过的++;
}while(howManyCorrect(winningNums,guessNums)<5);
System.out.println(“在消费“+currency.format(已消费)+”之后,您赢得了幻想5彩票$75000奖金!”);
总花费+=花费;
}
bothTotal=总花费+旧总计;
两次运行=运行+旧运行;
新平均值=(两者合计/两者合计);
PrintWriter fileWriter=新的PrintWriter(新文件(“average.csv”);
println(bothTotal+,“+bothRuns);
System.out.println(“\n中奖平均成本:”+currency.format(newAvg));
fileScanner.close();
fileWriter.close();
}
捕获(IOE异常)
{
System.out.println(e.getMessage());
}
}
公共静态void RandomlyAssignedNumber(int[]anyArray)
{
随机rng=新随机();
for(int i=0;i
Integer.parseInt()不喜欢小数点或任何字母字符。如果遇到alpha字符(甚至是空格),parseInt()方法将抛出NumberFormatException。如果文件中的数字是双精度数据类型数字的字符串表示形式,则使用double.parseDouble()方法将该数字转换为双精度数据类型变量。数据文件内容(或其中一部分)的视图当文件内容为:40000000,2时,它可以工作,但当它们更新为:1.44845504E8,4时,它会抛出一个NumberFormatException谢谢!作为双重身份的解析解决了这个问题。