• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do methods works?  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to methods, how do they work, I have been watching videos and reading on them for hours, but have yet to find an answer to how I actually use them.

on line 17 I call getMenuOption, with x inside the parameter. What exactly does x repsresent? I am trying to pass my menu selection input to the getMenuOption that was called in the main method.
however, when I go to make my switch statement, I use x within my parameters, should I be using variable x or variable menuselection?

 
Andrea Binello
Ranch Hand
Posts: 62
5
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:

You cannot do this, the x variable is not yet initialized and cannot be passed to getMenuOption before.
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing in an int named "menuSelection" to this method. Either use it in some meaningful way or don't pass anything at all. You are currently not doing anything with it.
Obviously a variable name "x" isn't descriptive (unless you are calculating coordinates). You are calling a method named getMenuSelection, what does it return?

Also, you should only create a Scanner for System.in *once* and then reuse it.

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 44 is supposed to be returning choice to the getMenuOption method call, however line 28 I receive an error variable choice cannot be found.

also my num1 and num2 operand inputs give me the error of not being initialized.
any suggestions on better passing within parameters

 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No parameters. This is good.
If getMenuOption() is no longer taking a parameter, why are you attempting to pass one in?


 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are repeating the same problem. You are passing in a variable "op" and then not using it. Only pass in the values you actually need to perform some operation.
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you call this method you are creating a new Scanner. In your entire application you should only open a Scanner to System.in ONCE.
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You are repeating the same problem. You are passing in a variable "op" and then not using it. Only pass in the values you actually need to perform some operation.

You seem to be under the misconception that in order to get something out that you have to pass it in first. This is not true.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have followed your suggestions carey, I did not understand how the method passing worked, so I just sort of passed things in different ways to see what would work

...now All of my return answers are not being resolved to a variable in my switch statements, any idea how to fix this part?

 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your add() method returns a result but you aren't doing anything with it. Essentially throwing it away. Two ways you could do this:
or


 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I have a majority of code good, other than my  choice cannot be resolved to a variable error for my choice variable ending the do while loop.

the input for choice menu selection is also invalid

--
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you've defined "choice" inside the "do" block. Outside of the block (which ends with a '}') the variable goes out of scope and is no longer visible. The while() expression is outside of the block, hence, cannot see "choice". (You're not the only one to get confused by this.) The solution is to define "choice" before you enter the "do" block.

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good eyes. Can you also see what may be causing the program to not return to the main menu after a case has executed, it simply moves on and completes the next case, even if I put break; at the end of each case.



 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding Scanner, if you declare and initialize it as a "static final" you can then access it anywhere without needing to pass it around. Note static-finals are constants and should be in all-caps.

 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each "case" block in a switch() statement should end with a "break". Not including the break is legitimate code but it just means that the execution just "falls through" to the body of the next case.
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:That's because you've defined "choice" inside the "do" block. Outside of the block (which ends with a '}') the variable goes out of scope and is no longer visible. The while() expression is outside of the block, hence, cannot see "choice". (You're not the only one to get confused by this.) The solution is to define "choice" before you enter the "do" block.

You didn't follow my example correctly. You still need to call getMenuOption() inside the do() loop.
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A style suggestion:
Usually the "case 1:" is on a line by itself and the body of the case indented one more level. And don't forget to add breaks.

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you get the output NaN if you attempt to divide by zero?

would you simply change the double to a float?
 
Carey Brown
Saloon Keeper
Posts: 3263
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divide by zero in float and double produces a NaN (Not-a-number) as a result. With integer arithmetic you'll get an ArithmeticException.
 
Campbell Ritchie
Marshal
Posts: 56225
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The details are in the Java® Language Specification (=JLS). It tells you what combinations will produce NaN,which ±∞ and which an exception.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay awesome, thank you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!