如何閱讀雙打我能夠讀取字符串變量,但它不會讀雙出於某種原因。我能做些什麼來使它讀取雙?從.txt文件
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();
}
}
}
你能舉個輸入文件的例子嗎? – Martinsos
嘗試讀取它爲'String',然後使用['Double#parseDouble']將其轉換爲'double'(http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html #parseDouble%28java.lang.String%29) –
錯誤是什麼? – Adi