2013-11-23 114 views
0

我搞砸了試圖打印出這個數組列表我在做什麼錯我試過多次,但是當它打印它輸出空。打印數組列表

import java.util.ArrayList; 
import java.util.Arrays; 
import java.text.NumberFormat; 
import java.util.Scanner; 

//Class header 

public class ShoppingCart { 

    // Start of main method 

    public static <Item> void main(String[] args) { 

     // Declare and instantiate a variable that is an ArrayList that can hold 
     // Product objects 

     ArrayList<Product> item = new ArrayList<Product>(); 

     // Declare necessary local variables here 

     String Name = null; 
     double Price = 0; 
     int Quantity = 0; 
     String Seller = null; 

     Scanner scan = new Scanner(System.in); 

     String Shop = "yes"; 

     // Write a print statement that welcome's the customer to our shop 

     /** 
      * 
      * create a do while that will be keep looping as long as user wants to 
      * continue shopping 
      */ 

     Product im = new Product(Name, Price, Quantity, Seller); 

     // do while loop start 

     do { 

      // Ask user to enter product name and store it in appropriate local 
      // variable 

      System.out.print("Please Enter the Product Name: "); 

      Name = scan.next(); 

      // Ask user to enter product price and store it in appropriate local 
      // variable 

      System.out.print("Please Enter the Price of the Product: "); 

      Price = scan.nextDouble(); 

      // Ask user to enter quantity and store it in appropriate local 
      // variable 

      System.out.print("Please enter the Quantity: "); 

      Quantity = scan.nextInt(); 

      // Ask user to enter product manufacturer name and store it in 
      // appropriate local variable 

      System.out.print("Please Enter the Manufacturer: "); 

      Seller = scan.next(); 

      System.out.print("Would you like to continue shopping?"); 

      Shop = scan.next(); 

      // create a new Product object using above inputed values 

      Product item2 = new Product(Name, Price, Quantity, Seller); 

      // add above created Product to the ArrayList cart if Product has 
      // available stock 

      // if stock not available inform user requested product is out of 
      // stock 

      // Ask user whether wants to continue shopping 

      // set the do while loop to continue to loop if Yes option is 
      // selected 

     } while (Shop.equals("Yes")); 

System.out.println("Product      Unit Price   Quantity   SubTotal"); 
System.out.println(Name + "    " +  (Price) + "  " + Quantity + "   " + (Price * Quantity)); 
System.out.println("00"); System.out.println(item); 

// do while loop end 
      // header for shopping cart contents 

// print details of each product added to the ArrayList 

// calculate total price of the shopping cart 

// print the total price of the shopping cart 


    }// end of main method 

     }// end of Shop class 
+1

請重新發布您的代碼,並使用'{}'按鈕,它的格式代碼塊而不是報價。 - 現在請僅顯示相關代碼(其中包括:刪除註釋和空白,我們對此不感興趣)。 –

+0

如果您從未向「item」列表中添加任何內容,那麼在您打印它時它將爲空。你期待別的什麼嗎? – DaoWen

回答

0

首先,你爲什麼這樣做

public static <Item> void main(String[] args) { 

public static void main(String[] args) { 

要打印的ArrayList只是這樣做

System.out.println("Name\tPrice\tQuantity\tSeller\tTotal") 
for (Product p : item){ 
    double total = p.price * p.quantity; 
    System.out.println(p.name + "\t" + p.price + "\t" p.quantity + "\t" + p.seller + "\t" + total); 
} 

但首先,你需要給列表添加一些東西

Product item2 = new Product(Name, Price, Quantity, Seller); 
item.add(item2); 

甚至更​​清潔的期待想法是覆蓋the toString方法在Product

public class Product { 
    String name; 
    String Seller; 
    double price; 
    int quantity; 

    public Product(String name, double price, int quantity, String seller){ 
     this.name = name; 
     this.seller = seller; 
     this.price = price; 
     this.seller = seller; 
    } 

    public String toString(){ 
     double total = p.price * p.quantity; 
     return name + "\t" + price + "\t" quantity + "\t" + seller + "\t" + total; 
    } 
} 

要從列表打印只是這樣做。

for (Product p : item){ 
    System.out.println(p); 
} 
+1

我不知道它是如此多_can't_只是_why ??? _它創建一個名爲'Item'的泛型類型參數,但它從來沒有使用。它_does_編譯,但它沒有任何意義。 – DaoWen

0

它,因爲你從來沒有ITEM2添加到ArrayList項目 只需添加

item.add(item2) 
0

有在你的程序相當多的錯誤。

首先,當您嘗試打印您的數組列表時沒有打印任何內容,原因是您從不向數組列表中添加任何元素,因此您必須使用以下方法。

arrayListName.add(elementToAdd); 

其次你的主要方法是不正確,而不是

public static <Item> void main(String[] args) { 

應該

public static void main (String[] args) {