如何从.txt文件中读取双精度数

时间:2013-03-28 21:23:23

标签: java

我能够读取字符串变量,但由于某种原因它不会读取double。我该怎么做才能让它读到双倍?

public class RundraiserApp
{

/**
 * @param args
 * 
 */
public static void main(String[] args)
{
    Fundraising[] dList = new Fundraising[10];

    String name = null;
    String address = null;
    String cityStateZip = null;
    double donation = 0;
    int i = 0, ctr = 0;

    Scanner in;
    File file = new File("Donations.txt");
    try
    {
        in = new Scanner(file);

        while (in.hasNext() && i < dList.length)
        {
            name = in.nextLine();
            address = in.nextLine();
            cityStateZip = in.nextLine();
            donation = in.nextDouble();
            i++;
        }
        ctr++;
    }
    catch (FileNotFoundException e1)
    {
        e1.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:1)

考虑下面的文件结构

Name
Address
Zip
2000.50

改变捐款,如。

donation = Double.parseDouble(in.nextLine());