| Author |
How do I "stitch" the toString with Main() to execute if statements together?
|
Sam Acropolis
Greenhorn
Joined: Sep 30, 2012
Posts: 16
|
|
Hey Guys so I finished writing this basic program to convert a number to base 2-10 . I created the IF statements in the toString method, and to get the base convesion using Integer.toString(String s, baseToConvertTo) . My issues is I can define toString 'static' and have it execute from Main and I have tried sending inputB and inputB to the toString via inputB.toString() ...sorry noob in Java. Is there a way when I run the program after I have typed after the prompts I can access the method and it will return me my desired result?
Thanks
|
 |
Andy Jack
Ranch Hand
Joined: Nov 22, 2012
Posts: 257
|
|
Sorry, I am not able to understand what you are saying. But, i will try to help you anyway.
line 19 causes the error: Cannot make a static reference to the non-static method toString() from the type BaseConvert
Since its an instance method or non static method, you have to create an object of type BaseConvert to be able to use that method.
If we make toString() static, we get error: This static method cannot hide the instance method from Object
So, try this
If you get compiler errors and things like that, then please post them in your question. Makes it easier for people to help you. If you do not give us enough info, then you probably won't get any response for a long time.
Good luck.
|
Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
|
 |
Sam Acropolis
Greenhorn
Joined: Sep 30, 2012
Posts: 16
|
|
Thanks I think Im very close ..but the issue now is a StackOverFlow error so pretty much lines 92-95 repeated indefinitely.
Sorry for not explaining Im pretty much trying to pass the Input N and InputB Variables into my toString so they will grab the number to be converted and use the the "base" I have supplied and convert it to that , and the program will check
what base using the IF statements I have provided
Thanks
|
 |
Sam Acropolis
Greenhorn
Joined: Sep 30, 2012
Posts: 16
|
|
This may be the culprit as to why their is a StackOverFlow
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Sam Acropolis wrote:
This may be the culprit as to why their is a StackOverFlow
What's the last thing you do in your toString method ?
|
Joanne
|
 |
Sam Acropolis
Greenhorn
Joined: Sep 30, 2012
Posts: 16
|
|
|
Print out the numbers the user typed , their converted number and then return the toString. Maybe the return is causing the problem probably....
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Sam Acropolis wrote:Maybe the return is causing the problem probably....
No. The call to toString is causing the problem. You call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString. The last thing this call to toString does is to call toString...
It's known as recursion.
What you should be doing is storing the value returned from your calls to Integer.toString() in a variable.
Then instead of printing out all that stuff at the end of your toString method you should be storing it in a String along with the value returned from Integer.toString() and then returning that String.
You then print out what toString returns in your main method.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9944
|
|
If we take all the chaff out of your toString method, we are left with this:
That line 4 is a call to a method. The method you are calling happens to be itself. Which then calls itself. Which then calls itself...
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Andy Jack
Ranch Hand
Joined: Nov 22, 2012
Posts: 257
|
|
fred rosenberger wrote:If we take all the chaff out of your toString method, we are left with this:
That line 4 is a call to a method. The method you are calling happens to be itself. Which then calls itself. Which then calls itself...
oh yes.
|
 |
Sam Acropolis
Greenhorn
Joined: Sep 30, 2012
Posts: 16
|
|
Ok I managed to come up with this and solves the recursion issue ... now the program seems to stop after the second prompt asking for the base to which to be converted .
|
 |
 |
|
|
subject: How do I "stitch" the toString with Main() to execute if statements together?
|
|
|