2

Alright so I'm trying to have the variables "totalTax" and "total" hold the price of the item, and they work fine when I call them within the method they are being set at, but when I call them from another class, it returns 0.

public class Input
{
private Scanner keybd;
private String[] costArray;
private String[] itemArray;
private double totalTax;
private double total;

/**
 * Constructor for objects of class Scanner
 * 
 * @param anyAmountofItems the amount of items you are going to be buying
 */
public Input(int anyAmountofItems)
{
    keybd = new Scanner(System.in);
    costArray = new String[anyAmountofItems];
    itemArray = new String[anyAmountofItems];
    totalTax = 0.0;
    total = 0.0;
}
/**
 * Mutator method to set the item names and costs
 * 
 * @param anyValue the tax rate percentage (ex. 0.08 for 8%)
 */
public void setArray(double anyValue){
    //System.out.println("Enter the sales tax percentage: ");
    //double salesTax = keybd.nextDouble();
    //for(int index=0; index < itemArray.length; index++){ 
    //System.out.println("Enter the item name: ");
    //itemArray[index] = keybd.next();}
    double totalTax=0.0;
    double total=0.0;
    for(int indexc=0; indexc < costArray.length; indexc++){
       System.out.println("Enter the item name: ");
       String anyName = keybd.next();
       itemArray[indexc] = anyName;
       System.out.println("Enter the item cost: ");
       double cost = Double.valueOf(keybd.next()).doubleValue();
       costArray[indexc] = " " + cost;
       totalTax = totalTax + (cost * anyValue);
       total = total + cost;
    }
    //for(int indexa=0; indexa < itemArray.length; indexa++){
    //System.out.println(itemArray[indexa] + "-" + costArray[indexa]);}
    //System.out.println("Sales Receipt");
    //System.out.println("--------------");
    //for(int i=0;i<costArray.length;i++){
    //    System.out.println(itemArray[i] + " - $" + costArray[i]);
    //}
    //returnCostArray();
    //System.out.println("Total tax: $" + totalTax);
    //System.out.println("Total cost pre-tax: $" + total);
    //System.out.println("Total cost including tax: $" + (total+totalTax));
    totalTax = totalTax;
    total = total;
}
public String returnArray(int anyElement){
    int index = anyElement - 1;
    return itemArray[index];
}
public double returnTotal(){
    return total;
}
public double returnTotalTax(){
    return totalTax;
}
public void returnCostArray(){
for(int i=0;i<costArray.length;i++){
    System.out.println(itemArray[i] + " - $" + costArray[i]);
    }
 }
 }

Then, I'm running this class which uses the Input class and it is returning 0, yet when I do the System.out.println in the same class that I already have, it works and returns the total.

public class TaxClass
{
private Input newList;
/**
 * Constructor for objects of class Tax
 * 
 * @param anyAmount Enter the number of items
 */
public TaxClass(int anyAmount)
{
    newList = new Input(anyAmount);
}
/**
 * Mutator method to add items and their cost
 * 
 * 
 * @param anyTax Enter the sales tax percentage
 */
public void addItems(double anyTax){
    double salesTax = anyTax;
    newList.setArray(salesTax);
    System.out.println("Sales Receipt");
    System.out.println("--------------");
    newList.returnCostArray();
    System.out.println("Total tax: $" + newList.returnTotalTax());
    System.out.println("Total cost pre-tax: $" + newList.returnTotal());
    System.out.println("Total cost including tax: $" +  (newList.returnTotal()+newList.returnTotalTax()));
}
//public String returnArray(){
    //newList.returnArray();
//}
}

EDIT: Local variable problem. I'm trying to use the "return array(int AnyElement)" method in the first class, but when I try to compile it is telling me that: returnArray(int) in Input cannot be applied to (). Any help? Thanks! Thanks!

1
  • do you think that throwing all the code here is a sensible move? Write some "minimal" code which exiguity the "strange" behavior and post it here. Commented May 7, 2011 at 15:05

3 Answers 3

8
totalTax = totalTax;
total = total;

Try...

this.totalTax = totalTax;
this.total = total;

The local variable inside that method takes dominance over the instance variable declared at the top of the class, so assigning totalTax = totalTax does nothing - it assigns the local variable in the method to itself. Using this.totalTax indicates that you want to use the instance variable instead.

You can use different variable names inside that method to avoid this sort of thing entirely.

(If you didn't intend to create new variables inside that method, refer to MByD's answer.)

Sign up to request clarification or add additional context in comments.

11 Comments

Alright, I'm also now having another problem, I'll edit it in so you can see!
Edited in - can you possibly help with this other problem also? Thank you!
Sounds like you're missing an argument to returnArray(int element). Pass in an integer - for instance, int value = returnArray(1); And remember to be careful that you have protection against stuff like returnArray(-1);, which will throw exceptions left right and centre.
You sure? I have this in input class: public String returnArray(int anyElement){ int index = anyElement - 1; return itemArray[index]; }
Then I'm calling it from the other class with this: public String returnArray(int anyElement){ newList.returnArray(); }
|
4

This:

double totalTax=0.0;
double total=0.0;

inside of a method shadows the class members called totalTax and total, remove the double when you use them inside a method:

totalTax=0.0;
total=0.0;

Comments

1

That's because you are setting local variables with the same name.

try

totalTax=0.0;
total=0.0;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.