2013-11-23 72 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) {