Java 数字格式中的空字符串
我的任务是: 以下是我的问题:Java 数字格式中的空字符串,java,arrays,file-io,casting,Java,Arrays,File Io,Casting,我的任务是: 以下是我的问题: 如何修复此错误: 线程“main”java.lang.NumberFormatException中的异常:空字符串 在sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1012)中 在java.lang.Double.parseDouble(Double.java:527) 位于extracredit.Main.readData(Main.java:72) 位于extracredi
import java.io.*;
import javax.swing.JOptionPane;
import java.util.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
String fname = "data.txt"; //Read in the data file for use in the array
String pass= JOptionPane.showInputDialog("Please enter the " +
"password to continue:"); /*Have the user enter the password to
access the file. */
checkPass(pass); // Verify that the password is correct before continuing.
readData (fname); // Read data, print output and save output file.
}
private static void checkPass (String pass)
{
String password= "INF260";
int passCount= 0;
if (pass.equals(password)) {
System.out.println("The password is correct. Continuing...");
}
else {
do {
pass= JOptionPane.showInputDialog("Please re-enter the" +
"password:");
passCount++;
} while (!pass.equals(password) && passCount < 2);
if (!pass.equals(password)) {
System.out.println("You have tried to enter the " +
"password too many times. Exiting...");
System.exit(0);
}
else {
System.out.println("The password is correct. Continuing...");
}
}
}
public static void readData (String data) throws IOException{
FileReader inputData= new FileReader (data);
BufferedReader findNum= new BufferedReader (inputData);
String str= findNum.readLine ();
int count=-1;
int countNum= 0;
double total= 0;
double min= 0;
double max= 0;
double average= 0;
FileWriter writeFile = new FileWriter("sales.txt");
PrintWriter printFile = new PrintWriter(writeFile);
while (str != null)
{
double num= Double.parseDouble (str);
if (count == 0){
countNum++; // counter of Reciepts to use
}
str = findNum.readLine();
}
double [][] input = new double [countNum][10];
total= getCurrentTotal(input); /*This will get the total
from the method getCurrentTotal.*/
min= getCurrentMin(input); /*This will get the minimum value from
the method getCurrentMin.*/
max= getCurrentMax (input); /*This will get the maximum value from
the method getCurrentMax.*/
average= (total / countNum); //Calculate the average.
System.out.println("The List of Today's Sales:");
for (int row = 0; row < input.length; row++){
System.out.println ();
System.out.println("Customer " + row + "\t");
for (int column = 0; column < input[row].length; column++){
if (input [row].length < 10){
System.out.println(input[row][column] + "\t");
str = findNum.readLine();
}
else{
System.out.println ("There are too many receipts" +
" for one Customer.\n");
System.exit (0);
}
}
}
System.out.println ("There are " + countNum + "receipts in the list.");
/*This will print the total of receipts in the list.*/
System.out.println ("The total of today's sales is $" + total); /*
This will print the total of the sales for the day.*/
System.out.println ("The average of today's sales is $" + average); /*
This will print the average sale total.*/
System.out.println ("The highest receipt is $" + max); /* This will print
the highest sale.*/
System.out.println ("The lowest receipt is $" + min); /* This will print
the lowest sale.*/
Date date = new Date();
System.out.println ("\n The current time is:" + date.toString()); /* This
will print the current date and time */
}
public static double getCurrentTotal (double [][] input){
double totalAmount = 0;
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
totalAmount += input [row][column];
}
}
return totalAmount;
}
public static double getCurrentMin (double [][] input) {
double currentMin = input[0][0];
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
if (currentMin > input[row][column])
currentMin = input[row][column];
}
}
return currentMin;
}
public static double getCurrentMax (double [][] input){
double currentMax = input[0][0];
for (int row = 0; row < input.length; row++){
for (int column = 0; column < input [row].length; column++){
if (currentMax < input[row][column]){
currentMax = input[row][column];
}
}
}
return currentMax;
}
}
import java.io.*;
导入javax.swing.JOptionPane;
导入java.util.*;
导入java.util.StringTokenizer;
公共班机{
公共静态void main(字符串[]args)引发IOException{
String fname=“data.txt”;//读取数据文件以在数组中使用
String pass=JOptionPane.showInputDialog(“请输入”+
“要继续的密码:”;/*请用户输入要继续的密码
访问该文件*/
checkPass(pass);//继续之前,请验证密码是否正确。
readData(fname);//读取数据、打印输出并保存输出文件。
}
专用静态无效检查过程(字符串过程)
{
字符串密码=“INF260”;
int passCount=0;
if(pass.equals(password)){
System.out.println(“密码正确,继续…”);
}
否则{
做{
pass=JOptionPane.showInputDialog(“请重新输入”+
“密码:”;
passCount++;
}而(!pass.equals(password)&&passCount<2);
如果(!pass.equals(密码)){
System.out.println(“您已尝试输入”+
“密码次数太多。正在退出…”);
系统出口(0);
}
否则{
System.out.println(“密码正确,继续…”);
}
}
}
公共静态void readData(字符串数据)引发IOException{
FileReader inputData=新的FileReader(数据);
BufferedReader findNum=新的BufferedReader(inputData);
字符串str=findNum.readLine();
整数计数=-1;
int countNum=0;
双倍合计=0;
双最小值=0;
双最大值=0;
双平均=0;
FileWriter writeFile=新的FileWriter(“sales.txt”);
PrintWriter printFile=新的PrintWriter(writeFile);
while(str!=null)
{
double num=double.parseDouble(str);
如果(计数=0){
countNum++;//要使用的接收计数器
}
str=findNum.readLine();
}
双精度[]输入=新双精度[countNum][10];
总计=getCurrentTotal(输入);/*这将获得总计
从方法getCurrentTotal*/
min=getCurrentMin(输入);/*这将从
方法getCurrentMin*/
max=getCurrentMax(输入);/*这将从
方法getCurrentMax*/
average=(total/countNum);//计算平均值。
System.out.println(“今天的销售清单:”);
for(int row=0;row输入[行][列])
currentMin=输入[行][列];
}
}
返回电流最小值;
}
公共静态双getCurrentMax(双[]输入){
双电流最大值=输入[0][0];
for(int row=0;row// from your main method
String fname = "data.txt";
readData (fname);
// the method being called
public static void readData (String data[][]){
BufferedReader br = new BufferedReader(new FileReader(data));