my dog learned polymorphism*
The moose likes Beginning Java and the fly likes While and Do-While Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "While and Do-While Question" Watch "While and Do-While Question" New topic
Author

While and Do-While Question

Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
Hi again Java Ranch!

Im currently working on a project that lets you add, subtract, multipy, or divide 2 integers, and then gives you the answer

I wanted to put a while or do-while switch on it so that when you get your answer,
it restarts itself and prompts you to pick which operand you want and which two integers to be used, but I havent been
able to figure it out.

Help and suggestions would be great! Please explain your logic behind your reasoning!

Here is my code so far!

--------------------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

What did you try and what specific problem are you having? I don't see a while or do/while in that code. Have you gone through a tutorial or textbook section on while and do/while loops? If not, that's your starting point. Then take your best shot, and then post a more specific question if you get stuck.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11441
    
  16

The real difference between a while and a do-while is the latter is guaranteed to run at least once. a while loop can be written so that it always runs at least once, by doing something like this:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Some poor code style there.
You have far too long a main method. The ideal length for a main method is one statement. You have a lot of repeated code, which ought to be refactored into separate methods. Some of it should be refactored into a utility class (that example has different methods from what you want).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
If you are going to use a switch-case block with exit as an option, you can put it inside a while(true) loop, but I doubt the design of a loop which exits from inside itself.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
To augment on Campbell's response: repetitious code is a typical beginner's mistake. We all did it when we were starting out, so it's no badge of shame -- just something to start learning to recognize and correct. See that the code in each case of the switch block is almost identical. The only difference is the operation that is performed to determine what value is assigned to answer (and the corresponding symbol that is printed out). Try to spot repetitions like this in your code, and see if there is a way that all the common code could be consolidated. If you change your perspective on this problem somewhat, you may find that you don't need the switch block at all.
Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
Thanks for all your help! Ended up figuring it out myself!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Well done How did you manage it?
Chris Hathaway
Greenhorn

Joined: Feb 08, 2012
Posts: 16
Well I got to theory crafting and started trying random stuff, I put the whole code, give or take a few brackets, in a do - while loop, and made sure the "Int choice = 0;" was outside the loop, final code looks like this...

I remember you saying
1. that you doubted the design of a do - while switch that exits from inside itself
2. that my main method was way too long, and alot of repeated code.

Do you think you could elaborate for future reference? Would be very helpful

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
Consider Paul's rocket mass heater.
 
subject: While and Do-While Question