2012-05-23 138 views
-1

好吧,輸入文本文件有如下格式:讀取文本文件中的數據,並打印到文本文件的Java

product  sugar 
boughton 13-3-2012 
useby  12-12-2012 
boughtat $3.56 
quantity 5 

product  sauce pan 
quantity 10 
boughtat $19.99 
boughton 13-3-2012 

product  sugar 
boughton 23-3-2012 
quantity 15 
boughtat $2.99 
useby  1-10-2012 

product  plate 
quantity 10 
boughtat $-19.99 
boughton 13-3-2012 

product  orange juice 
boughton 33-3-2012 
quantity 15 
boughtat $2.99 
useby  1-10-2012 

我的代碼: DataIn.java

class DataIn { 

    public static ArrayList<Product> getData(String filename) { 
     ArrayList<Product> recordlist = new ArrayList<Product>(); 
     int index = -1; 
     try { 
      File inFile = new File(filename); 
      Scanner reader = new Scanner(inFile); 
      String s; 
      Product p = null; 

      while (reader.hasNext()) { 
       s = reader.nextLine(); 
       Scanner line = new Scanner(s); 
       String cmd; 
       if (line.hasNext()) { 
        cmd = line.next(); 
        if (cmd.equalsIgnoreCase("product")) { 
         index++; 
         p = new Product(); 
         p.setName(line.nextLine()); 
         recordlist.add(index, p); 
        } else if (cmd.equalsIgnoreCase("boughton")) { 
         Calendar d; 
         d = ConvertToCal(line.nextLine()); 
         p.setBoughtOn(d); 
         recordlist.set(index, p); 
        } else if (cmd.equalsIgnoreCase("useby")) { 
         Calendar d; 
         d = ConvertToCal(line.nextLine()); 
         p.setUseBy(d); 
         recordlist.set(index, p); 

        } else if (cmd.equalsIgnoreCase("boughtat")) { 
         if (line.hasNextDouble()) { 
          p.setBoughtAt(line.nextDouble()); 
          recordlist.set(index, p); 
         } 
        } else if (cmd.equalsIgnoreCase("quantity")) { 
         if (line.hasNextInt()) { 
          p.setQuantity(line.nextInt()); 
          recordlist.set(index, p); 
         } 
        } else { 
         System.out.println("Error: no command"); 
        } 
       } 
      } 

      reader.close(); 


      return recordlist; 
     } catch (Exception e) { 
      System.out.println("Error" + e.getMessage()); 
      return null; 
     } 
    } 

    public static Calendar ConvertToCal(String s) { 
     try { 
      Date d; 
      DateFormat f = new SimpleDateFormat("dd-MM-yyyy"); 
      d = (Date) f.parse(s); 

      Calendar cal = Calendar.getInstance(); 
      cal.setTime(d); 
      return cal; 
     } catch (ParseException e) { 
      System.out.println("Exception: " + e); 

     } 
     return null; 
    } 

    // method to print the output in the file 
    public static void writeData(ArrayList<Product> productlist, String fileName) { 



     if (productlist.size() == 0) { 
      System.out.println(" no product"); 
      return; 

     } 

     try { 
      File fileOut = new File(fileName); 
      PrintWriter out = new PrintWriter(fileOut); 
      for (Product p : productlist) { 
       out.println("product " + p.getName()); 
       out.println("boughtOn " + (p.getBoughtOn().get(Calendar.DATE))); 
       out.println("useBy " + (p.getUseBy()).get(Calendar.DATE)); //ERROR in here 
       out.println("boughtAt " + p.getBoughtAt()); 
       out.println("quantity " + p.getQuantity()); 
      } 

      out.close(); 
     } catch (FileNotFoundException e) { 
      System.out.println("file not found"); 
     } 

    } 
} 

Product.java

//products class 
public class Product { 
    private String name; 
    private Calendar boughtOn; 
    private Calendar useBy; 
    private double boughtAt; 
    private int quantity; 
    // first product class constructor 
    public Product(){ 
    name=null; 
    boughtOn=null; 
    useBy=null; 
    boughtAt=0; 
    quantity=0; 
} 
    //second product class constructor 
    public Product(String pName, Calendar bOn, Calendar uBy, double bAt, int qt){ 
    name= pName; 
    boughtOn=bOn; 
    useBy=uBy; 
    boughtAt= bAt; 
    quantity=qt; 
} 
    //method to get the name of product 
    public String getName(){ 
     return name; 
} 
//method to get the date that product bought on 
    public Calendar getBoughtOn() { 
    return boughtOn; 
} 
    //method to get the date that product use by 
public Calendar getUseBy() { 
    return useBy; 
} 
//method to get the purchase price of product 
    public double getBoughtAt() { 
     return boughtAt; 
} 
    //method to get the quantity of the product 
    public int getQuantity() { 
    return quantity; 
    } 
    //method to change the product name 
public void setName (String n) { 
name=n; 
} 

    //method to change the date that product bought on 
public void setBoughtOn(Calendar bo) { 
     boughtOn=bo; 



} 
//method to set the date that product will expired at 
    public void setUseBy(Calendar ub) { 
    useBy=ub; 
} 
    //method to set the purchase price of product 
    public void setBoughtAt(double ba) { 
    boughtAt= ba; 
} 
//method to set the product quantity 
    public void setQuantity(int q) { 
    quantity=q; 
} 
    public String toString(){ 
     return name +"\t" + boughtOn + "\t" + useBy + "\t" + boughtAt +"\t" + quantity; 
    } 
    public static void main(String []args){ 

     ArrayList<Product> list; 
     list=DataIn.getData("products_1.txt"); 
     DataIn.writeData(list,"save1.txt"); 
}  

} 

線程「main」中的錯誤異常java.lang.NullPointerException

in this line out.println(「useBy」+(p.getUseBy())。get(Calendar.DATE));在Java代碼中DataIn.java

我想不通,所以我張貼在這裏整個事情,國防部請重新格式化代碼,我不知道如何當我避免該行做到這一點

而且通過使用//,程序可以運行,但我在我出去把文件只顯示像(鮑和boughtAt有問題)

product  sugar 
boughtOn 13 
boughtAt 0.0 
quantity 5 
product  sauce pan 
boughtOn 13 
boughtAt 0.0 
quantity 10 
product  sugar 
boughtOn 23 
boughtAt 0.0 
quantity 15 
product  plate 
boughtOn 13 
boughtAt 0.0 
quantity 10 
product  orange juice 
boughtOn 2 
boughtAt 0.0 
quantity 15 

很抱歉,如果代碼太長,但很難解釋我的問題。

+0

您是否嘗試過調試? – sathis

+3

它不是「複製粘貼你的代碼,以便其他人可以爲你調試」網站。做一些調試,將問題縮小到一兩行,併發布顯示問題的最小代碼和數據 – Bohemian

+0

您的期望是什麼? – Prathap

回答

0

您的輸入列表中的某些產品缺少「useby」字段,因此您的錯誤是正常的,因爲某些Product對象具有NULL useby字段。

+0

你說得對,所以我試圖避免這一行,然後輸出文件不是我所期望的.. –

相關問題