【问题标题】:Empty string in number formatting数字格式中的空字符串
【发布时间】:2010-10-20 11:26:57
【问题描述】:

This 是我的任务:

这是我的问题:

  1. 如何解决此错误:

线程“主”java.lang.NumberFormatException 中的异常:空字符串 在 sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1012) 在 java.lang.Double.parseDouble(Double.java:527) 在 extracredit.Main.readData(Main.java:72) 在 extracredit.Main.main(Main.java:27)

  1. 此程序是否还有其他问题?

到目前为止,这是我的代码:

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

【问题讨论】:

  • 我认为使用 Stack Overflow 来为您的作业提供如此专业的帮助是不合适的。如果您在上课时遇到困难,请与助教交谈并自学,然后向 Stack Overflow 寻求帮助,以解决您已投入时间但无法解决的狭隘问题。
  • 我的课没有任何问题,这只是一个额外的学分作业。我的教授实际上向这个网站推荐了我们的课程。我发布了我已经编写的代码,因为我不仅仅是在寻求答案,而且我真的想要帮助解决这些问题。这只是一个非常基础的初级编程课程,我们还没有学会如何做上面介绍的一些事情。我只是认为在比我自己更好的程序员的帮助下学习会更好

标签: java arrays file-io casting


【解决方案1】:

最好的解决办法是:

  • 学习你的课程材料
  • 从问题的一个子集开始,例如读取文件。
  • 测试一下
  • 循环:
    • 继续改进和增强计划,直到满足所有要求。
    • 测试一下
  • 交出

【讨论】:

    【解决方案2】:
    // 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));
    

    我们这里不兼容。

    • fname 是一个字符串
    • 该方法采用 String[] 作为参数。
    • 构造函数 newFileReader() 采用字符串,而不是二维数组。

    这三个都应该是相同的数据类型。

    如何将每个“收据”用零分隔(如上图链接所示)?

    您不必这样做。您必须从其中包含零的文件中读取。
    我建议你写一个类似这样的方法:

    public double[] readOneReceipt(BufferedReader reader);

    这个方法应该

    • 逐行读取,直到遇到 0 条目
    • 对于它读取的每个条目,将值转换为数字(双精度?)
    • 将号码存储在临时结构中。
    • 遇到“0”时,创建一个大小正确的新数组并将读取的值复制到其中。

    如何将输出写入单独的文件?

    使用 java.io.FileWriter

    这个 IMO 最难的一点是,您被告知将数据存储在 2d 数组中,但在您读取数据之前,您并不知道该数组的大小。 这意味着您要么使用临时动态结构,要么读取文件两次 - 一次找出有多少收据以便您可以制作数组,然后再次实际读取收据数据。

    【讨论】:

    • 感谢您的帮助 evnafets,我正在努力修复它。 -Ava
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 2018-06-02
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多