我正在尝试使用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());
}
}
答案 0 :(得分:5)
您的计划中还有其他一些错误。
首先,您应该向ArrayList
添加类型。由于您尝试添加int
,double
和String
,我建议您创建ArrayList<Object> lInvoice = new ArrayList<Object>() ;
然后用你的迭代器循环:
ListIterator<Object> ltr = lInvoice.listIterator();
while(ltr.hasNext()){
out.println(ltr.next());
}
答案 1 :(得分:3)
您实际上不会在while
循环中打印任何内容,因为您的println()
回调超出了循环的范围。修复如下:
ListIterator<String> ltr = lInvoice.listIterator();
while(ltr.hasNext()) {
out.println(ltr.next());
}
答案 2 :(得分: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()
清除它,否则您将错过您真正想要的输入。