Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I "stitch" the toString with Main() to execute if statements together?

 
Sam Acropolis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Sam Acropolis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This may be the culprit as to why their is a StackOverFlow
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Sam Acropolis
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic