0

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());
    }
}
A4L
  • 17,353
  • 6
  • 49
  • 70
Blank1268
  • 123
  • 3
  • 7
  • 14
  • What's the error you get for the invoice part number; it looks like it could work, assuming Invoice has a `public void setPartNumber(String partNumber)` – Gus May 22 '13 at 21:25
  • You should remove the `;` after `while(ltr.hasNext())`, it is just an empty statement witch consumes your while loop. – A4L May 22 '13 at 21:28
  • Does this answer your question? [Ways to iterate over a list in Java](https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java) – Aswin Prasad Dec 02 '19 at 10:47

3 Answers3

5

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()); 
       }
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • @Blank1268 and for fun and giggles, did you know the `foreach` operator uses iterators behind the scenes? Try out `for (Object o : lInvoice) { out.println(o); }` – Porkbutts May 22 '13 at 21:55
3

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());
}
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • This wasn't my question, but I have to ask - how does the ListIterator work if it is parameterized to accept Strings when some of the things being added to the ArrayList it traverses are ints. Are they automatically parsed? – Andrew Martin May 22 '13 at 21:21
  • Since you have `ArrayList()` which is not parametrized, they are all Objects, actually. And when you print them, the `toString()` method is invoked. – Konstantin Yovkov May 22 '13 at 21:23
  • 1
    @kocko If you try to run the code, you will get a `ClassCastException` when looping with the `iterator` because the iterator expects objects as String (as its parametized). – Alexis C. May 22 '13 at 21:25
  • since the list in raw the `listIterator` has also to be raw for casting to work – A4L May 22 '13 at 21:27
  • 1
    @ZouZou: That's what my initial thought was. Should really have just run the code to check for myself! Thanks. – Andrew Martin May 22 '13 at 21:28
1

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:

  • I'm using a list of Invoice instead a list of strings, and then printing out each one
  • Scanner.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.
Gus
  • 3,534
  • 1
  • 30
  • 39