• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling variables between methods  RSS feed

 
Bob Jinkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm supposed to use two methods to calculate whether a patient gets a discount on their insurance but I'm having trouble passing the variable between the two methods. I need to pass the String insurance from displayWelcome to determineDiscount. I think I'm close but I'm not there yet. I'm getting two error messages, both on line #10.

"Error: ')' expected"

and

"Error: illegal start of expression"

Any ideas how to make it work?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
You are missing a few things.

1) Your first method needs to return a String
2) You need a variable named insurance in main
3) You don't mention the type (String) when calling a method. Only when declaring it.

Let us know if you need more detail on any of these three.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the following code has two bugs in it. Can you find either of them?

(you are off to a good start).


 
Bob Jinkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I got some different error messages now. I guess that progress. :P

Now the compiler says
"method determineDiscount cannot be applied to given types: determineDiscount();
required: String
found: no arguements
reason: actual and formal argument lists differ in length"
on line 10

and

"incompatible return types: unexpected return value"
on line 24.




Jeanne Boyarsky wrote:Also, the following code has two bugs in it. Can you find either of them?

(you are off to a good start).




Yeah, I noticed I forgot the copay variable about 30 seconds after I posted it. :/
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you have got the idea of indenting code for better readability right, except you haven't indented this code right:



It should really be indented like this:



And it's better if you put braces around all of your blocks, even though it's okay to leave them off when there's only one statement controlled by an if-clause. Like this:


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one of the two things in that code. The other is that

should be


In Java, comparing a String with == doesn't always work so isn't a good idea.

And on your how to pass variables question, you are getting closer. The first method has to store the answer in that variable and the second has to pass it.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob, welcome to the Ranch, nice to see you around

It seems you know how to create methods already, which is very good. Another thing what you could start following is good variables, methods naming. It is important.
Beside all that, think about a method as about a guy who is responsible for one job only.
If you have method displayWelcome(), it suppose to print welcome message and that is it.

Analyse example below, I mostly used your already written code (some left for you to finish), just defined different program structure. Notice, how method run defines your all program flow. You read those 4 lines and it is pretty clear what this program is all about. Try to stick to it, and see how it goes. Notice how I changed your variable insurance to insuranceProvider, it is way more clearer, as this variable actually is meant to store insurance provider.

If you're not familiar with the content in line 4, don't worry too much now, just keep like this, and define method run as I showed you in example (you can think of other name if you like, but then change it to the same on line 4 instead run).

Also note, getUserInput method i defined with no arguments, if you think you haven't got enough challenge, try to pass prompt text as an argument to getUserInput method rather hardcoding it to method. I have in mind, try to achieve:
insuranceProvide = getUserInput("Question you want to ask user");
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!