• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issues declaring variables  RSS feed

 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been given the following exercise and I and TOTALLY stuck on how to proceed. I think the main issue is how I've declared my variables.

In plain English what I'm trying to do is get someone to choose one of three payment methods by using if/else statements. In my if/else statements I need to have this outcome:
( payMethods == 1)( or Cash)
( payMethods == 2)(or Credit/Debit )
( payMethods == 3)( or Cancel)

I've been coding for a month so I'm using JOptionPane to implement this. Suggestions?

 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless

You aren't going to get many responses if you expect someone to go through a big assignment and read your whole program.
What, exactly, is the problem?

Is it compiling? Can you run it? Are you getting an error?
 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main issue is in the way I declared my variables. I think if I got that correct I could do the rest. I'm not sure how to do it. I want

the integer cashOption to both equal 1 and equal cash. Is that possible?

What I have:
int cashOption = new cash (1);

isn't working.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. are you really declaring a cash object?
2. if so, is the name of the "cash" class capitalized?
3. did you create a constructor that takes an int?

The correct way to instantiate an object (assuming the constructor takes whatever argument) is:

 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been programming for all of four weeks, so I don't know what I'm doing. I'm looking to declare cashOption as an int and instantiate it so that it can also be equal to Cash. But when I put
int cashOption a = new cashOption("Cash");

in my compiler it returns incorrect. I still don't get it.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want it to be an int,



int is a primitive type, not an object. you don't need to instantiate them, just declare.

if cash is also an int, you can do this:


hope that helps!

P.S. I edited the title here so you'll get more help
If you use a meaningful subject, you'll get more people looking!
 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In plain English What I'm trying to do is get someone to choose one of three payment methods by using if/else statements. In my if/else statements I need to have this outcome:
( payMethods == 1)( or Cash)
( payMethods == 2)(or Credit/Debit )
( payMethods == 3)( or Cancel)
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In words, this is what you need to do:

1. get input from the user as a number (1,2,3).... you might use radio buttons.
2. take that input, save it to a variable, and do a switch / case:

 
Crea Smith
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate all of your help but after four weeks of coding, we haven't learned anything about switching or making radio buttons yet. I'll keep trying though with what we've learned so far. Thanks again.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out about the switch statement in the Java™ Tutorials. You will also find about radio buttons if you explore the tutorials.
Some of their descriptions are very brief, and you may prefer what you have in your books.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you knew a bit more object-orientation, you would set up an enumerated typeYou can use the members of that enum in a switch block.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This doesn't make sense. the 'int' means you are creating an int primitive. according to the language spec, what follows must be a variable name, so it thinks 'cashOption' is just that - a variable name. you then have an 'a', which I'm guessing is causing a syntax error when you compile. It would help GREATLY if you would post your actual code and the exact error you are getting, because otherwise this is nothing more than a WAG.

How exactly ARE you getting the input from the user? are you getting a string or an integer? Or could you get either? if you are getting an integer, are you getting it as an int or as a string containing the character "1"?

How you answer these questions will determine how you proceed.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!