0

我正在尝试使用 ListIterator 从 ArrayList 打印,我很确定我做错了,因为它不起作用但我不知道如何修复它。所以抓取零件号的那条线不起作用,不知道为什么;P。任何帮助总是受到赞赏:)。

package invoice;
import static java.lang.System.out;

import java.util.*;

public class InvoiceTest {


    public static void print(){

    }

    public static void main (String args[]) {

        Scanner imput = new Scanner (System.in);
        ArrayList lInvoice = new ArrayList() ;
        int counter = 0;
        int partCounter;

        out.println("Welcome to invoice storer 1.0!");
        out.println("To start please enter the number of items: ");
        partCounter = imput.nextInt();


        while (counter < partCounter){
            counter++;
            out.println("Please enter the part number:");   
            Invoice invoice1 = new Invoice(); //Makes invoice 1 use the invoice class
            String partNumber = imput.nextLine();// sets part number to the next imput
            //invoice1.setPartNumber(partNumber);// Sets it to the private variable in invoice.java
            lInvoice.add(partNumber);

            out.println("Please enter in a discription of the part: ");
            String partDis = imput.nextLine();
            //invoice1.setPartDis(partDis);
            lInvoice.add(partDis);

            out.println ("Please enter the number of items purchased: ");
            int quanity = imput.nextInt();
            //invoice1.setQuanity(quanity);
            lInvoice.add(quanity);

            out.println ("Please enter the price of the item:");
            double price = imput.nextDouble();
            //invoice1.setPrice(price);
            lInvoice.add(price);

        }

        ListIterator<String> ltr = lInvoice.listIterator();
        while(ltr.hasNext());
        out.println(ltr.next());
    }
}
4

3 回答 3

5

您的程序中还有其他一些错误。

首先,你应该在你的ArrayList. 由于您尝试添加int,doubleString,我建议您创建一个ArrayList<Object> lInvoice = new ArrayList<Object>() ;

然后用你的迭代器循环:

ListIterator<Object> ltr = lInvoice.listIterator();
       while(ltr.hasNext()){
           out.println(ltr.next()); 
       }
于 2013-05-22T21:22:02.933 回答
3

实际上,您不会在while循环中打印任何内容,因为您的println()回调超出了循环的范围。像这样修复它:

ListIterator<String> ltr = lInvoice.listIterator();
while(ltr.hasNext()) {
   out.println(ltr.next());
}
于 2013-05-22T21:17:43.670 回答
1

戴上我的通灵调试器帽子,我猜你是想打印一个订单项发票。我对 Invoice.java 的内容做了一些假设,但我猜下面的代码是你真正想要的:

    Scanner imput = new Scanner(System.in);
    ArrayList<Invoice> lInvoice = new ArrayList<Invoice>();
    int counter = 0;
    int partCounter;

    out.println("Welcome to invoice storer 1.0!");
    out.println("To start please enter the number of items: ");
    partCounter = imput.nextInt();
    imput.nextLine();//skips the rest of the line (carriage return)

    while (counter < partCounter) {
        counter++;
        out.println("Please enter the part number:");
        Invoice invoice1 = new Invoice(); // Makes invoice 1 use the invoice
                                            // class
        String partNumber = imput.nextLine();// sets part number to the next
                                                // imput
        invoice1.setPartNumber(partNumber);// Sets it to the private
                                            // variable in invoice.java

        out.println("Please enter in a discription of the part: ");
        String partDis = imput.nextLine();
        invoice1.setPartDis(partDis);

        out.println("Please enter the number of items purchased: ");
        int quanity = imput.nextInt();
        imput.nextLine();
        invoice1.setQuanity(quanity);

        out.println("Please enter the price of the item:");
        double price = imput.nextDouble();
        imput.nextLine();
        invoice1.setPrice(price);
        lInvoice.add(invoice1);
    }

    ListIterator<Invoice> ltr = lInvoice.listIterator();
    while (ltr.hasNext()) {
        Invoice next = (Invoice)ltr.next();
        out.println(next.getPartNumber()+"\t"+next.getPartDis()+"\t"+next.getPrice()+"\t"+next.getQuanity());
    }

有趣的变化:

  • 我使用的是一个列表Invoice而不是一个字符串列表,然后打印出每个
  • Scanner.nextInt()将从其输入中留下回车,因此您必须调用nextLine()以清除它,否则您将错过您真正想要的输入。
于 2013-05-22T21:50:53.670 回答