• 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

Main Method calling or repeating

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there, hope everyone doing well. I was preparing for OCAJ exam and trying some codes so i can grasp methods properly as this is the most important part of all topics. anyway, coming to my problem with this code. I have found this code online ans trying to modify it if I can. I wanted a simple change with the code which is it will ask every time i finish a calculation that "do you want to calculate more ?". (its like user will again choose an option for calculation). then the main method will repeat until user enter the first number as 0(ZERO). I know it will work just fine with loops such as while or may be if else. but how i can tell the main method to repeat itself. (sounds silly but am still learning... he he ). Please help. Thank you in advance, you guys are very kind here.



 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things before I address the question: You should always QuoteYourSources (← that's a link). Where did you get this code? It's not all that great. I would rewrite it if I were you.

But to answer your question. You have basically three choices for loops: for/next, while, and do/while. I know a lot of people here don't like the do/while but I think it's perfect for this situation. How would you use a do/while loop to get main() to repeat?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code looks messy...

The repeat() method will not work. In that method, you are first creating a variable choice that is initialized to 0 and the next thing you do is check if it is not 0. That is ofcourse always false. Also, it makes no sense to do add(), sub(), mult(), div() all after each other. You didn't want the user to do an add, then a sub, etc. When you read or write code, think in logical steps. What's happening in the repeat() method is not logical at all.

Get rid of the repeat() method.

Put a loop in the main() method, around almost everything in the method. You could for example use a do-while loop.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Personally I don't think, that such a training with code like this would help you score higher in OCAJ exam. OCAJ exam is not about such a things how logic of the program should flow. It is more about the Java language itself, technical part of it, compiler behaviour - in different words, unusual situations, corners of the language.
Do you have an exam preparation book? Have you looked at it?

About that code you posted:
Probably it more shows how the code shouldn't be written.

I mean (do you notice what's possibly wrong with it?):
And there are many more problems. Usually such a situations suggests one thing - bin it and start over.
Likely with a pencil and piece of paper by writing down the logical steps in simple english what has to be done (by omitting "how" part).
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And never close a Scanner pointing to System.in. Not even if Eclipse suggests it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic