2

I am developing a simple POS program. Which I have a couple loops to add the part information to a list (invoiceList), then all the parts ordered under another list (invoiceListMain) and then all the invoices ordered into a main list (invoiceLogList). So essentially I have List within List within List.

This works when I output the whole list in a print statement. But I need to be able to unpack the list.

[[738, [113, 5.0, 43.45], [248, 2.0, 12.78]], [459, [248, 10.0, 63.9], [113, 1.0, 8.69]]]

I get this error

ArrayListDemo.java:136: error: incompatible types: String cannot be converted to ArrayList for(ArrayList line : invoiceLogList)

when I do:

for(int i=0; i < invoiceLogList.size(); i++)
         {
           for(ArrayList<String> line : invoiceLogList)
           {
              System.out.print(" \n" + line.get(0));
           }

         }

Before negative ratings, please leave a reason why.

From researching, I think it has to do with how I had to compile all my list within list within list.

invoiceLogList.add(invoiceListMain.toString());

I had to add .toString() in order to get this to work. So my guess is I can't unpack this as an Array because it is no longer an array? It's a list? I'm not 100% sure.

Does anyone have advice on a better way or how to unpack the data within?

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;


public class ArrayListDemo {

   public static void main(String[]args){

      //Part 1
      ArrayList<String> part1 = new ArrayList<String>();

      part1.add("06011");     
      part1.add("Castrol EDGE 0W20 Motor Oil - 1 qt");
      part1.add("9.49");

      //Part 2
      ArrayList<String> part2 = new ArrayList<String>();

      part2.add("248");     
      part2.add("Castrol EDGE 5W30 Motor Oil - 1 qt");
      part2.add("6.39");

      //Part 3   
      ArrayList<String> part3 = new ArrayList<String>();

      part3.add("03050");
      part3.add("Castrol Hypuron 10W30 Motor Oil - 1 gal");
      part3.add("6.39");

      //Part 4
      ArrayList<String> part4 = new ArrayList<String>();

      part4.add("113");
      part4.add("Castrol Pyroplex Protection ES Multi-Purpose Grease - 14 oz");
      part4.add("8.69");

      //Part 5
      ArrayList<String> part5 = new ArrayList<String>();

      part5.add("020");
      part5.add("Castrol GTZ ULTRACLEAN 5W20 Motor Oil - 6 gal");
      part5.add("69.99");


      //adding parts to main Part List
      ArrayList<ArrayList<String>> mainPartList = new ArrayList<ArrayList<String>>();

      mainPartList.add(part1);     
      mainPartList.add(part2);
      mainPartList.add(part3);
      mainPartList.add(part4);
      mainPartList.add(part5);


//-----INVOICE INPUT----------------------------------------------------------------------------

      //create a new scanner
      Scanner input = new Scanner(System.in);

      //Invoice Input
      String a = "";
      String invoiceNumber = "";
      String p = "";
      double q = 0.0;
      double partPrice = 0.0;
      double partSum = 0.0;
      String partNum = "";
      String partDescrip = "";
      String c = "";

      //Invoice Array

      ArrayList<String> invoiceLogList = new ArrayList<>();//Stores all invoices created
         do{//loop to create invoice

            ArrayList<String> invoiceListMain = new ArrayList<>();//stores individual invoices

            Random generator = new Random();//creates random invoice number     
            int sequence = generator.nextInt(1000);
            invoiceListMain.add(String.format("%03d", sequence));//invoice number
            System.out.print("Invoice #: " + sequence);

            do{//loop to add parts to invoice

               ArrayList<String> invoiceList = new ArrayList<String>();//stores part#, qty, sumTotal

               System.out.print("\n\nPart Number: ");
               p = input.next();

               //fetch part info from array mainPartList
               for(ArrayList<String> part : mainPartList)
               {
                  if(part.contains(p))
                  { 
                    for (int i = 0; i < 1; i++)
                       partNum = part.get(0);   
                       partDescrip = part.get(1);
                       invoiceList.add(partNum);//add part# to list
                       System.out.print("\t" + partNum + " - " + partDescrip);
                       partPrice = Double.parseDouble(part.get(2));//grabs price of part in list                   
                  }
               }

               System.out.print("\nQuantity: ");
               q = input.nextDouble(); //asks quantity sold
               invoiceList.add(Double.toString(q));//add quantity to list

               partSum = partPrice * q;
               invoiceList.add(Double.toString(partSum));//add part sum to list
               System.out.printf("\t $%.2f",partSum);

               System.out.print("\n\n---Add to cart?: ");
               c = input.next();

               invoiceListMain.add(invoiceList.toString());//adding part order to mainList

               }while(c.equalsIgnoreCase("Y")); 


             //Create Invoice Loop
             System.out.print("\n\nCreate a new Invoice? ");
             a = input.next();

             invoiceLogList.add(invoiceListMain.toString());

             }while(a.equalsIgnoreCase("Y"));



          //Print all invoices
          for(int i=0; i < invoiceLogList.size(); i++)
          {
            for(ArrayList<String> line : invoiceLogList)
            {
              System.out.print(" \n" + line.get(0));
            }

          }
   }
}
12
  • 1
    What is invoiceLogList? What is invoiceListMain? Commented Apr 29, 2018 at 14:21
  • when you say unpack? do you mean change the List<List<List<String>>> to a List<String> ? Commented Apr 29, 2018 at 14:21
  • invoiceListMain - Arraylist holding all the products ordered my the user Commented Apr 29, 2018 at 14:25
  • 1
    Yes, your problem is the invocation of toString(). Why did you need to do that? Commented Apr 29, 2018 at 14:28
  • 2
    @Sam Right, don't use two, three separate lists to maintain related data, instead create a class with the required properties, overriding toString, then you can have a List<SomeType> where SomeType is the class name. Commented Apr 29, 2018 at 14:29

3 Answers 3

1

i try that example and that what i get maybe that what u want !

ArrayList<ArrayList<ArrayList<String>>> invoiceLogList= new ArrayList<ArrayList<ArrayList<String>>> ();
    ArrayList<ArrayList<String>> invoiceListMain= new ArrayList<ArrayList<String>>();
    ArrayList<String> c = new ArrayList<String>();
    c.add("13");
    c.add("50");
    c.add("43.45");
ArrayList<String> d = new ArrayList<String>();
d.add("728");
invoiceListMain.add(d);
invoiceListMain.add(c);
invoiceLogList.add(invoiceListMain);

  for(int i=0; i < invoiceLogList.size(); i++)
         {
           for(ArrayList<ArrayList<String>> line :invoiceLogList)
           {
              System.out.print(" \n" + line.get(0));
           }

         }

Output:

[728]
BUILD SUCCESSFUL (total time: 0 seconds)
Sign up to request clarification or add additional context in comments.

1 Comment

well damn, there's my issue. I think with some tweeking this will help all my problems. I didn't know that I needed all the ArrayList<ArrayList<ArrayList
0

It's because you are accessing the same list in both loop headers, I believe making this correction should fix the issue:

for(int i=0; i < invoiceLogList.size(); i++)
         {
           for(ArrayList<String> line : invoiceLogList.get(i))
           {
              System.out.print(" \n" + line.get(0));
           }

         }

1 Comment

I get this error: for-each not applicable to expression type
0

If you look at your print out,
you can see that 738 and 459 are one level too high.

Here is a break down by depth:
list with 2 elements
the 1st contains a list of 3 elements that are:
* a string 738
* a List of Strings [113, 5.0, 43.45]
* a List of Strings [248, 2.0, 12.78]

This was suppose to be a list of lists (from your story).
this is what the exception is telling you.

1 Comment

It is a list of lists if I had entered more than 1 invoice. 738 is the invoice number which contains the parts numbers ordered which is [113, 5, 43.45][248, 2, 12.78]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.