2009-04-20 193 views
0

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

    我不認爲這是適當的使用堆棧溢出來得到這個專門的幫助你的任務。如果你正在和一個班級苦苦掙扎,那就和自己一起學習,然後轉向堆棧溢出(Stack Overflow)尋求幫助,解決一些狹窄的問題,這些問題你已經投入了時間,但卻無法弄清楚。 – RossFabricant 2009-04-20 02:04:30

    +0

    我在班上沒有任何問題,這只是額外的功勞。我的教授實際上向我們的網站推薦我們的課程我發佈了我已經寫過的代碼,因爲我不只是要求答案,而且我真的想要解決這些問題。這只是一個非常基礎的開始編程課程,我們還沒有學會如何做上面提到的一些事情。我只是覺得在比我更好的程序員的幫助下學習會更好 – 2009-04-20 02:37:36

    回答

    1
    // 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
    • 的方法,以一個String []作爲參數。
    • 構造函數newFileReader()接受一個字符串,而不是2d數組。

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

    How can I separate each "receipt" with zero (like shown in the image link above)?

    您不必這樣做。您必須從該文件讀取其中的零。
    我會建議你寫一個像這樣的方法:

    public double [] readOneReceipt(BufferedReader reader);

    這個方法應該

    • 讀一行行,直到遇到一個0項
    • 它讀取轉換值,每次進入一個數(雙?)
    • 將號碼存儲臨時結構體。
    • 當您遇到「0」時,請創建一個正確大小的新數組,並將讀取的值複製到其中。

    How can I write the output into a separate file?

    隨着java.io.FileWriter

    這IMO最難位的事實是,你被告知將數據存儲在一個二維數組,但你不知道什麼大小在讀取數據之前製作數組。 這意味着您要麼使用臨時動態結構,要麼讀取文件兩次 - 一次找出有多少個收據,以便您可以創建數組,然後再次實際讀取收據數據。

    3

    最好的解決辦法是:

    • 研究你的課程材料
    • 開始與像是剛剛讀取文件的問題的一個子集。
    • 測試
    • 遍歷:
      • 不斷改善和提高程序,直到它滿足所有要求。
      • 測試
    • 一方面它在