Reputation: 123
I am trying to print from an ArrayList using an ListIterator, i'm pretty sure i'm doing it wrong because it's not working but I don't know how to fix it. All so the line that grabs the part number isn't working, not sure why ;P. Any help is always appreciated :).
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());
}
}
Upvotes: 0
Views: 7927
Reputation: 93882
There is some other errors in your program.
First, you shoud add a type to your ArrayList
. Since you're trying to add int
, double
and String
, I recommend you to create an ArrayList<Object> lInvoice = new ArrayList<Object>() ;
Then just loop with your iterator :
ListIterator<Object> ltr = lInvoice.listIterator();
while(ltr.hasNext()){
out.println(ltr.next());
}
Upvotes: 5
Reputation: 3554
Putting on my psychic debugger hat, I'm guessing you meant to print out a line-item invoice. I'm making some assumptions about the contents of Invoice.java, but I'm guessing the below code is what you really wanted:
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());
}
Interesting changes:
Invoice
instead a list of strings, and then printing out each oneScanner.nextInt()
will leave the carriage return from its input, so you have to call nextLine()
to clear it, or you'll miss the input you really wanted.Upvotes: 1
Reputation: 62874
You actually don't print anything within the while
loop, because your println()
callback is out of the scope of the loop. Fix it like this:
ListIterator<String> ltr = lInvoice.listIterator();
while(ltr.hasNext()) {
out.println(ltr.next());
}
Upvotes: 3