• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a Method to the Main  RSS feed

 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have been reading about methods and I do have a beginner level understanding on how they work. I was trying to mess around and make a dog calculator using methods. I ran into a small snag; I cannot get the method to call to the main or the program to compile correctly. The first code below is the original. To me it looks like (based off of some examples I looked at) there should be no problems, but NetBeans gives me a few errors. 1) line 8- "cannot find symbol, variable dogYearCalc; 2) line 18 illegal start of expression; and 3) line 22 - unreachable statement.



Someone told me that I was calling dogYearCalc without any arguments in your main method. I take that to mean that I needed to add it to the main, so I did here:


That got rid of my first error, but then when I tried to run the program NetBeans said that I have no main class, so switched back to the original program above.
I thought that when I calling the dogYearCalc method on line 10 was the whole purpose of using a method. It seems to me that putting it somewhere in the main is counter productive. This is a little confusing for me so if you need me to try to explain better please ask. Thank you in advance!

Kris

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dogYearCalc is a method, so you need to call it with the required parameters in parentheses, like "dogYearCalc(8.0)".

The dogYearCalc method also has a bunch of problems, like an extra semicolon and missing round parentheses in line 18, and a missing return statement at the end.

Once that all works, you need to figure out to get the parameter value to pass into the dogYearCalc method from the input dialog, but start by getting the basics working.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who on earth told you to add additional parameters to a main method?

You have some serious potential errors in that code. One is using the == operator with doubles. It will only work if the numbers are exactly equal. What will happen if you pass 1.5 for example? It will even fail if you try it with 0.1 * 10 Remember whenever you stray into the world of doubles, you are in a world of imprecision and not‑quite‑exact results. I suggest you get rid of the two ifs and change them to an if‑else. Much simpler solution, too.

The next thing I think you are doing wrong is writing too much code all at once. Then you are getting poor advice and guessing about the solution. You are also not indenting the code correctly. I suggest you look at our suggestions about formatting. Also enter your code backwards as shown in that link. That makes it much easier to get the {} matched properly. You think your braces are in certain locations, but they are actually somewhere different. The compiler doesn't see the incorrect indentation and simply believes you about the braces, so you end up with code which looks different from what it really is, and doesn't do what you think.

Start with a main method which simply prints out that it is running, nothing else. Remove all the other code and add it back bit by bit. Then at least you can see where things are going wrong. Add it one or two lines at a time. Start off with the dog's age method with this as its body:-
return 123.45;
When you can reliably print 123.45, then you can consider doing some calculations and get rid of the 123.45.

Why are you using Option Panes for user input? That is very old‑fashioned coding. The input dialogue doesn't return a number, but a String. You would have to parse that String to a double. I suggest you try a Scanner. There is an example in the documentation which reads an int from the keyboard; you can easily change that to a double; read through the list of methods.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Who on earth told you to add additional parameters to a main method?

To be fair to the other person, I think it was more a case of Kris misunderstanding what he was told
Kris French wrote:Someone told me that I was calling dogYearCalc without any arguments in your main method

which, as Ulf has explained, is accurate.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right Joanne. Thank you.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the push in the right direction. I am still working out the kinks. There a few things that I just don't understand that I am trying to understand. For example, I know that the main method is supposed to call from the dogYearCalc method and that method is doing all of the calculations and sending them to the main. I guess it is the finer details of getting those calculations to the main is where I am running into a wall. For those of you who helped and is still curious, here is where I stand now:



Thank you again!

Kris
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that just an excerpt? Since "humanAge" is not defined in main, it wouldn't compile.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

I thought I was calling humanAge to the main from the dogYearCalc method. I take it from your question that is wrong. I really do not know what exactly to put in the parenthesis. I was thinking that the variable humanAge would have been appropriate.

Kris
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use humanAge in dogYearCalc because it's defined there (as a parameter passed in from main), but it's not defined in main itself. Something like "double humanAge = 30.0;" in main would solve that. (Eventually you'll want to obtain the value of humanAge from the user input, of course, but you should start by getting a compilable and runnable program.)
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

I was glad to see you responded; that's exactly what I did. Now I'm trying to figure out how to take the user input without the "double humanAge = 30.0;" actually getting added into the if statement. Is there some sort of generic way to declare without messing up the math? Thank you!

Kris
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
without the "double humanAge = 30.0;" actually getting added into the if statement. Is there some sort of generic way to declare without messing up the math?

This sounds odd - there is no if statement in the main method (which is what you should have been altering - the dogYearCalc method was OK).

The javadocs of the JOptionPane class show how to get the user input from showInputDialog. It's a String object, which you'll have to convert to a double - the java.lang.Double class has methods that can do that.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds odd - there is no if statement in the main method (which is what you should have been altering - the dogYearCalc method was OK).

The javadocs of the JOptionPane class show how to get the user input from showInputDialog. It's a String object, which you'll have to convert to a double - the java.lang.Double class has methods that can do that.


I have seen examples of this while trying to figure this out, but have no idea how, or when, to use the java.lang.Double class. would it be something like:



 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would separate it into two steps: First, obtain the user input as string. The javadocs have the exact line of code you need to use for that. Then, convert it to a double via Double.parseDouble assign it to "humanAge" (or pass it directly into the dogYearCalc method).
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try
double age = Double.parseDouble(JOptionPane.showInputDialog("Please enter age: "))
…but only if you read all four of those links

Ulf is right. Simply pass 12.5 or whatever to the calculate dog age method and consider the keyboard input when you have got all that working.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for helping me understand; I'm almost there. Unfortunately I have a class project due and I have spent a littler too long trying to figure out this problem, so I have to set this aside for now. I am going to finish this program, but probably not until next week when I have more time. I don't want you to feel that I have wasted your time; I WILL finish this. I value your experience and will most definitely have more questions in the very near future and do not want to burn these helpful bridges ;).

Thanks again!

Kris

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem. Just let us know here once you've got it done (or if you have more questions). It's always good to hear of people making progress.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kris French wrote:I don't want you to feel that I have wasted your time; I WILL finish this.

I don't think most people here would feel that way, whether you finish this or not. We're here to help you LEARN. And you can learn a LOT even if you don't finish this.

I'm not trying to DISCOURAGE you from finishing it...but don't feel you have to for our sake.
 
Kris French
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came back to this today. I went line by line in the code, went through all the posts and spent some time reading through javadoc on the oracle site. For the second time Campbell's subtle nudge in the right direction caused me to smack my forehead. I had most everything just about there, but when I relaxed, looked at the code logically and did a little research (guided by all of you) the solution was so simplistic I feel embarrassed for being thick-headed. Thank you all for being understanding, and most of all, making me do it on my own! Here is the final result:


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for updating the site with your finished code.

One thought though - what happens if when asked "Please enter you dog's age in human years:" someone enters "two"
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or what if somebody enters 1.5?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!