Jump to content

Help with int cannot be dereferenced error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Cool Runnings

Cool Runnings

    Newbie

  • Members
  • Pip
  • 7 posts
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

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
woodType is an int. It does not have those methods.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Cool Runnings

Cool Runnings

    Newbie

  • Members
  • Pip
  • 7 posts
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
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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!