• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do I use a variable in an if statement?  RSS feed

 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how do I use a variable in an if statement to return a public void method from another class?

ive tried;
System.out.print("select 1 - 6: ");
Scanner myScanner = new Scanner(System.in);
myScanner.nextInt();

if (myScanner.nextInt()==1) {
carSelection colour = new carSelection();
colour.carSelection();

the method colour in class car selection is just 4 print line statements?
ive // out the if statement to see if it is correctly calling the method and displaying the data and it is?

so not sure what the problem is

thanks in advance, i will probably kick myself when i see how simple the solution is!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:Hi, how do I use a variable in an if statement to return a public void method from another class?

I'm afraid I don't understand the question.

Your code, specifically:appears to be using carSelection as both an object and a method. Now this might be possible - if your carSelection object also contains a method called carSelection() - but it seems unlikely; and also violates the general standard of class names always starting with CAPITAL letters.

My suggestion: back up and explain what you want to do, not how you're doing it (which plainly isn't working).

Winston
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I should have used CarSelection for class and the method is colourSelection.

so it should have read

CarSelection colourSelection = new CarSelection();
colour.carSelection();

all im simply trying to do, is get the user to input an int, 1-6, which will select the method from the class CarSelection.

if this is possible?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:so it should have read
CarSelection colourSelection = new CarSelection();
colour.carSelection();

No it shouldn't. Look again. Computer languages (and Java is no exception) require you to be precise, so be very careful when you're writing; otherwise you'll constantly be dealing with compiler errors.

all im simply trying to do, is get the user to input an int, 1-6, which will select the method from the class CarSelection.
if this is possible?

Sure it is. Have a look at the switch statement. Or (possibly better) use an enum to hold your colours.

Winston
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Winston,

would you mind pointing out what was wrong with the statement then, as it compiled and ran fine without any errors. it was just the if statement that wasn't working.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:would you mind pointing out what was wrong with the statement then, as it compiled and ran fine without any errors. it was just the if statement that wasn't working.

In which case you must have defined a field called 'colour' somewhere else, because in isolation:
CarSelection colourSelection = new CarSelection();
colour.carSelection();

defines a variable called 'colourSelection' and then attempts to call a method on one called 'colour'.

Winston
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is just a case of changing to a switch? like so?

System.out.print("select 1 - 6: ");
Scanner myScanner = new Scanner(System.in);
myScanner.nextInt();

switch (myScanner.nextInt()) {
case '1': CarSelection colour = new CarSelection();
colour.carSelection();
break;

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:so is just a case of changing to a switch? like so?

Sort of, but assuming that, for example, '1' means "red", you have to come up with some mechanism for making sure that your method call returns a RED colour object. I'm also a bit confused by the name CarSelection - what is this class supposed to represent?

Winston
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston,

would you mind showing me a simple switch statement that calls a class and then a public void method within that class. the method has no return value, just a couple of println statements.

as for the numbers,
1 - calls a method in that class to be displayed then the same for 2,3,4 etc, they all return a method(for now) that returns a few println statements.

does that make sense?

im not too worried about the objects and data for now, im just trying to get the program working so I can see it in action then go from there.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:
switch (myScanner.nextInt()) {
case '1': CarSelection colour = new CarSelection();
colour.carSelection();
break;


It would probably be a good idea to save the value into a variable before using it. In this case, what if the switch didn't match a case, then what? Its value is lost after the switch statement.

Henry
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Downham wrote:
switch (myScanner.nextInt()) {
case '1': CarSelection colour = new CarSelection();
colour.carSelection();
break;


It would probably be a good idea to save the value into a variable before using it. In this case, what if the switch didn't match a case, then what? Its value is lost after the switch statement.

Henry


Hi Henry, i was going to put in an ignore variable if the user inputs anything other than the int list. i was just showing a small example.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:im not too worried about the objects and data for now, im just trying to get the program working so I can see it in action then go from there.

Erm ... that doesn't sound like a great way to proceed. I can understand wanting to eliminate compiler errors so you can actually run the thing; but (at least from what you were saying) you have logic errors, and those have everything to do with the data and the objects you're working with.

Winston
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
John Downham wrote:im not too worried about the objects and data for now, im just trying to get the program working so I can see it in action then go from there.

Erm ... that doesn't sound like a great way to proceed. I can understand wanting to eliminate compiler errors so you can actually run the thing; but (at least from what you were saying) you have logic errors, and those have everything to do with the data and the objects you're working with.

Winston


Hi Winston, the programme I have written so far has no compiler errors, and no runtime errors, except for the fact that when I input the number 1, to call the class and method, nothing happens. like i said before i commented out the if and now the switch statement just to check that the main() could call then class and method, and it did. so now i need to work out the issue of the if or switch statement being able to call the class and method.
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I might have found my answer, I believe its down to the input method, im just reading up now about using a BufferedReader instead of a Scanner object.

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:except for the fact that when I input the number 1, to call the class and method, nothing happens.

That's because you're calling Scanner.nextInt twice (lines 3 and 5) and ignoring the value returned by the first call.
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
John Downham wrote:except for the fact that when I input the number 1, to call the class and method, nothing happens.

That's because you're calling Scanner.nextInt twice (lines 3 and 5) and ignoring the value returned by the first call.


.....there she is!! thank you very much sir!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:so now i need to work out the issue of the if or switch statement being able to call the class and method.

No. You need to get your program working the way you want it to.

Sorry if this sounds pedantic, but it's an important point. You're worrying about bits and bytes, and "how do I code this?" and "how does switch work?", when what you should be worrying about is what you want your program to do.

Reading between the lines (and you'll have to tell me if I'm wrong), your CarSelection class would appear to be a bunch of things that you might want to select (colour, trim, options, etc) when creating your "customized" vehicle, of which Colour is only ONE possibility. I'm not quite sure why (or even if) you chose this structure, but it's a reasonable way to go. Now you simply have to come up with a way to achieve it.

Since I don't yet know if I'm on the right lines, the only thing I can say at the moment is: read up about Java enums, because they sound to me like exactly what you might want.

HIH

Winston

[Edit]: it seems you've got your current "answer", but I leave the post for when you run into your next problem.
 
John Downham
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Winston, ill definitely be looking into Enums soon. I got it to work with the switch statement after removing the second scanner object. and using an int i; i = myScanner.nextInt(); then switch(i)
case 1 method
case 2 method 2 etc....
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Downham wrote:Thanks for your reply Winston, ill definitely be looking into Enums soon.

You're most welcome. Be assured that when I say these things, I'm not trying to put you down or sound 'superior'; it's just (I suspect) that I've been at this lark for a bit longer, so I know that it's easy to get caught up in the minutiae of the process - and it's usually not a good idea.

Luckily, objects (or rather, classes) have all sorts of mechanisms to help you out of this. Don't know how to calculate a logarithm? Or a fractal? Or any number of other things you might run into? Make its method abstract (or simply have it throw UnsupportedOperationException) and deal with it later. Everything else you write will still compile - and in many cases run - so you can deal with things incrementally.

Much of programming has to do with avoiding trivial matters. It's the design you want to get right, not (strangely enough) the code. There are plenty of maths or physics grads around who will be more than happy to code your fractal() method when the time comes. The reason you get paid the big bucks is for knowing when to hand it off to them.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!