java - 数字格式中的空字符串

标签 java arrays file-io casting

This我的任务是:

这是我的问题:

  1. 如何修复此错误:

Exception in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1012) at java.lang.Double.parseDouble(Double.java:527) at extracredit.Main.readData(Main.java:72) at extracredit.Main.main(Main.java:27)

  • 您还发现该程序存在其他问题吗?
  • 这是我到目前为止的代码:

        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;
        }
    }
    

    最佳答案

    最好的解决办法是:

    • 研究类(class) Material
    • 从问题的一个子集开始,例如读取文件。
    • 测试一下
    • 循环:
      • 继续改进和增强该计划,直到其满足所有要求。
      • 测试一下
    • 交出

    关于java - 数字格式中的空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/766590/

    相关文章:

    c - 程序跳过用户输入(C 语言的大型模块化图书馆信息系统)

    c - 将 int 分配给数组元素时出现段错误

    c# - 处理大量文件

    java - 为什么我的项目无法编译?

    java - 删除节点时,为什么不需要将该节点的 next 设置为 null?

    java - 写一个从 servlet 到 JavaScript 的数组响应?

    java - 用 Java 将非常小的字符串写入文件的最快方法是什么?

    java - 加载文件以及转换为十六进制时出现内存问题

    java - 何时使用 "new"关键字 android API

    java - 尝试在 Selenium WebElement 中选择特定数字