Beginner programmer needs help understanding how I received the int cannot be dereferenced error code on the program below. User is suppose to pick the wood type 1 for Pine 2 for Oak & 3 for Mahogany. Pine cost $100, Oak $225 & Mahogany $310. Program is to determine the cost of the table. Also if user enters invalid wood code, set the price to $0.00. Any advise would be greatly appreciated. I'm sure I have some stupid mistakes so please bare with me. Program is pasted first, errors after.
import java.util.Scanner;
public class Furniture
{
public static void main(String[] args)
{
int woodType;
String chosenCode = "";
double chosenCost = 0;
Furniture selectedFurniture = new Furniture();
final int PINE_CODE = 1;
final int OAK_CODE = 2;
final int MAHOGANY_CODE = 3;
final double PINE_COST = 100;
final double OAK_COST = 225;
final double MAHOGANY_COST = 310;
boolean choiceIsGood = true;
Scanner input = new Scanner(System.in);
System.out.println("What type of wood do you want ");
System.out.println("Enter " + PINE_CODE + " for Pine " +
OAK_CODE + " for Oak, or " + MAHOGANY_CODE + " for Mahogany... ");
woodType = input.nextInt();
if(woodType == PINE_CODE)
chosenCost = PINE_COST;
else
if(woodType == OAK_CODE)
chosenCost = OAK_COST;
else
if(woodType == MAHOGANY_CODE)
chosenCost = MAHOGANY_COST;
else
choiceIsGood = false;
if(choiceIsGood)
{
woodType.setCode(chosenCode);
woodType.setCost(chosenCost);
}
else
System.out.println("You entered " + woodType + " which is invalid.");
System.out.println("Wood choise: ");
System.out.println("Type: " + woodType.getType() +
" Table: " + woodType.getWood() + " Cost is " + woodType.getCost());
}
}
Error messages:
Furniture.java:34: int cannot be dereferenced
woodType.setCode(chosenCode);
^
Furniture.java:35: int cannot be dereferenced
woodType.setCost(chosenCost);
^
Furniture.java:40: int cannot be dereferenced
System.out.println("Type: " + woodType.getType() +
^
Furniture.java:41: int cannot be dereferenced
" Table: " + woodType.getWood() + " Cost is " + woodType.getCost());
^
Furniture.java:41: int cannot be dereferenced
" Table: " + woodType.getWood() + " Cost is " + woodType.getCost());
^
5 errors
Help with int cannot be dereferenced error
Started by Cool Runnings, Jan 28 2010 05:57 PM
3 replies to this topic
#1
Posted 28 January 2010 - 05:57 PM
|
|
|
#2
Posted 28 January 2010 - 08:01 PM
woodType is an int. It does not have those methods.
#3
Posted 29 January 2010 - 02:11 AM
So what should I do then? How do I change it to make it work? I understand woodType is an int, but how does it affect the methods? Do I need to change the "set & get"? if so...to what? If not, what else could I change to make it work?
#4
Posted 29 January 2010 - 02:16 AM
What ARE the setCode and setCost methods, anyway? You can only directly manipulate woodtype as an integer, such as with mathematical or bitwise operations, or to pass it to methods that require an integer. You can't attempt to use a method it has because integers do not have any methods associated with them.
Wow I changed my sig!


Sign In
Create Account

Back to top









