int cannot be deffered error
Hello,
I hope this is an appropriate forum for this question. This is not a HW assignment; I have just started Java as a hobby. I think the real problem I have is a lack of sound understanding of how to call methods, and what criteria have to be met to call a method within the main().
Here is the code:
public class InconvenientChange3
{
public static int quarters;
public static int dimes;
public static int nickels;
public static int pennies;
public static int sum;
public static int sum()
{
sum = quarters + dimes + nickels + pennies;
}
public static void main(String[] args)
{
System.out.println();
int[] values = new int [100];
for (int amount = 1; amount < 100; amount ++)
{
quarters = amount/25;
int remainder = amount%25;
dimes = remainder/10;
remainder = remainder%10;
nickels = remainder/5;
pennies = remainder%5;
}
for (int amount = 1; amount < 100; amount ++ )
{
values[amount] = sum.values[amount];
System.out.println(values[amount]);
}
System.out.println();
}
}
The error I get is: int cannot be dereferenced values[amount] = sum.values[amount];
I have tried troubleshooting this for a while, but can't figure out this error. Thanks so much for your assistance.
- steve
[1420 byte] By [
steve6375a] at [2007-10-2 4:37:20]

"Dereference" means when you use whatever notation the particular language provides to use a pointer to access what the thing points at. In Java's case, it's when you use the dot to get to a member vaiable or method of a reference, or when you use the [] to get to an element of an array.
String str = "abc";
int len = str.length();
We dereference the str variable with str.length() to get to the length method of the String object that str points to.
Primitive types--byte, char, short, int, long, float, double, boolean--are not references. They don't point at objects and can't be dereferenced.
sum is an int. It has no members that you can access with the dot. What are you trying to do with sum.values[amount]?
Hello,
Thanks for your response. My intent is to apply the method "sum" (which adds the total number of quarters, dimes, nickels, and pennies) to the elements of the values[ ] array, thus creating a new array that holds the number of coins required for each value.
So the value at index 1 is 1 (1 penny)
The value at index 11 is 2 (1 dime, 1 penny)
The value at index 41 is 4 (1 quarter, 1 dime, 1 nickel, 1 penny)
etc.
I played with this a little, and successfully printed the sum for every value by including the statement:
sum = quarters + dimes + nickels + pennies wihin the first for loop. (So I know my math logic is correct). Now I want to assign these sums to a new array - that's my intent.
Thanks again for your help and patience.
Regards,
steve
> Hello,
>
> Thanks for your response. My intent is to apply the
> method "sum"
Oh. You have both a method and a variable named sum. Ick. Rename one of them. Or if you're not actually using the variable, then just get rid of it.
Call sum like so: sum();
I haven't looked at your code closely enough to know whether sum needs to take a parameter or if it operates on member variables. You really should go through that tutorial though, start to finish.