1

I am working on a project that has a class called Items and a method called totals that calculates a grand total for an array of Items objects. for some reason it cant see Items, I know that I am missing something simple or obvious but I just cant figure it out. Ant help would be appreciated.

 public void totals(){

    int index=0;
   for (Iterator it = items.iterator(); it.hasNext();) {
       Items i = it.next();
       double itotal;
        itotal = items.get(index).Items.getTotal();
   }
}

and here is the Items class

public class Items {
 public String name;//instance variable for item name
 public int number;//instance variable for number of item
 public double price;//instance variable for unit price
 public double total;//instance variable for total
  Items(String name,int number,double price){
    this.name=name;
    this.number=number;
    this.price=price;
    total=number*price;
}
public void setName(String name){
     this.name=name;
 }
 public void setNumber(int number){
     this.number=number;
 }
 public void setPrice(double price){
     this.price=price;
 }
 public void setTotal(){
     total=number*price;
 }
 public String getName(){
     return name;
 }
 public int getNumber(){
     return number;
 }
 public double getTotal(){
     return total;
 }
 public double getPrice(){
     return price;
 }

Thanks in advance for the help.

2
  • 1
    Can you paste any errors that are happening? "It can't see Items" isn't very technical or specific to the problem. Commented May 12, 2015 at 2:49
  • this is the error that I get when I compile Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.Object.getTotal at ReciptPrinter.ReciptPrinter.totals(ReciptPrinter.java:52) at ReciptPrinter.ReciptPrinter.main(ReciptPrinter.java:36) Java Result: 1 the IDE gives this error Cannot find symbol symbol: method getTotal() Location: variable items of type Object Commented May 12, 2015 at 2:58

2 Answers 2

2

Basically, there are two flaws:

  1. you never increment the itotal variable and it's declared inside the loop
  2. you never access the variable i in the current iteration

And also, shouldn't your totals method return something (like itotal)?

The way I see it, the proper way of iterating over that items array is

public double totals(){
    double itotal = 0.0;    //#A
    for (Iterator<Items> it = items.iterator(); it.hasNext();) {   //#B
       Items i = it.next();   //#C
       itotal += i.getTotal(); //#D
    }
    return itotal; //#E
}

Basically:

  • #A Here you initialize the itotal variable (outside of the loop) that will contain the grand total for all items
  • #B You start iterating over all items
  • #C You get the next item in the array
  • #D You increment the grand total with the current item's total
  • #E You return the grand total
Sign up to request clarification or add additional context in comments.

4 Comments

This got me much closer, thanks i still have an issue with the line: Items i = it.next(); it is giving the error "incompatible types: Object cannot be converted to Items"
My bad, I've updated my answer. Provided your items list is declared as List<Items> the iterator declaration can be declared as Iterator<Items> and the items will have the proper type within the loop.
Oh WOW! now I feel like a total noob (which I am) but that got me going. THANK YOU!
Glad you found your way ;) Thanks!
0

There are a number of potential issues here.

In your for loop, you declare Items i, but never use it. Maybe it = it.next() should be a part of the for loop instead?

You call items.get(index), but index is always 0. You might want to use it here instead.

You declare double itotal and assign it within the for loop, so it's overwritten on each iteration. Maybe you want to declare it with an initial value outside the loop, and then increment it inside the loop.

Comments

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.