• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This may be the culprit as to why their is a StackOverFlow
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic