簡體   English   中英

數字格式的空字符串

[英]Empty string in number formatting

是我的任務:

這是我的問題:

  1. 如何解決此錯誤:

線程“主”中的異常java.lang.NumberFormatException:空sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1012)處的字符串,extracredit.Main.readData處的java.lang.Double.parseDouble(Double.java:527)處的字符串(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;
    }
}

最好的解決方案是:

  • 學習你的課程資料
  • 從問題的子集開始,例如僅讀取文件。
  • 測試一下
  • 循環:
    • 繼續改進和增強該程序,直到它滿足所有要求。
    • 測試一下
  • 交上
// 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()需要一個字符串,而不是2d數組。

所有這三個都應該是相同的數據類型。

如何將每個“收據”都設為零(如上面的圖片鏈接所示)?

不用了 您必須從該文件中讀取零。
我建議您編寫這樣的方法:

public double [] readOneReceipt(BufferedReader reader);

這種方法應該

  • 逐行讀取直到遇到0條目
  • 對於讀取的每個條目,將值轉換為數字(雙精度?)
  • 將號碼存儲在臨時結構中。
  • 遇到“ 0”時,請創建一個具有正確大小的新數組,並將讀取的值復制到其中。

如何將輸出寫入單獨的文件?

使用java.io.FileWriter

該IMO最難的一點是,系統會告訴您將數據存儲在2d數組中,但是在讀取數據之前,您並不確定要創建數組的大小。 這意味着您要么使用臨時動態結構,要么讀取文件兩次-一次找出有多少收據,以便創建數組,然后再次實際讀取收據數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM