• Post Reply Bookmark Topic Watch Topic
  • New Topic

End While Loop  RSS feed

 
Jeremy Olsen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I tell the user to enter something exactly without my program going into groundhog day loop. It is a simple answer but it has been eluding me for hours, please help me before I go completely bonkers.

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to make your choice inside your while loop, so if the answer is not right, it can loop again to get the right answer, but if it is correct you can exit.

what you have now when you make a wrong choice is basically this:
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all don't write these conversion code into main method. Just create an object of JCalc class in main method. Make two separate methods of converting Square feet to Tsubo and vice versa. Then you can also write another third method for menu like 1. Convert Square feet to Tsubo. 2. Convert Tsubo to Square feet 3. Exit from where user can choose what operation they want to perform and call this menu method from main method. Make use of switch case for selection of menu choice and call appropriate method from there. If
* 35.58; and / 35.58;
is fixed constant value never gonna change then you can store this value in a final float variable. Declare all variables in class.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If 35.58 value is fixed one, make the variable final static float for that and also make static method for menu selection as both of them will be same for all objects you create, so same variable and method will be shared among all objects of JCalc class which saves memory.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Olsen wrote:How do I tell the user to enter something exactly without my program going into groundhog day loop. It is a simple answer but it has been eluding me for hours, please help me before I go completely bonkers.

The basic problem is that you're trying to do everything in one place - and it's a habit you should try to get out of as fast as possible.

Think about what you're program is doing:
  • It's prompting a user to enter something.
  • It's taking what they entered (good or bad) and converting it to a type you can deal with.
  • It's then converting those values into something else, based on some formulas you've been given.
  • Finally, it prints the result of that last conversion.

  • Is it any wonder that you're losing you're mind?

    Deal with ONE THING AT A TIME, and don't write a single line of code for the next task until you KNOW that all the previous ones work. EVERY TIME.

    And break those tasks up; if not into classes, at the very least into methods.

    HIH

    Winston
     
    Jeremy Olsen
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is what I had and it worked every time, Winston. I was just trying to get a toLowerCase to work without success, then I tried to get a != to work.

     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    As
    Winston Gutkowski wrote:The basic problem is that you're trying to do everything in one place - and it's a habit you should try to get out of as fast as possible. , And break those tasks up; if not into classes, at the very least into methods.
    Please first make separate methods for them then you will understand the flow and where to apply what validation and all. Writing program and running successfully to get desired output, anyone can do but writing code which is clean and well structured such that any new person should easily be able to understand what and how it does . Even If you want to make changes later in it, It wouldn't be that tedious then.
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeremy Olsen wrote:

    If you write comments, make sure they tell you truth. Currently all of of them are misleading or just simply wrong.

    Line 3. It is just a variable declaration, it doesn't give any allowance for converting.
    Line 4. 1st - you're not initializing variable, you declaring. 2nd - you don't have variable Tsubo, but you do have numTsubo.
    Line 5. You are declaring variable, not initializing there. And you don't have such variable as Feet.
    Line 6. You are declaring variable, not initializing there.
    Line 16. You are not assigning value, you initializing (Initialize - give first value, assign - every other value).
    Line 17. Incorrect. Based on your comment, line should look like "answer = numTsubo", but you have "answer = numTsubo * 35.58".
    Line 22. Same mistake as per Line 16.
    Line 23. Same mistake as per Line 17.

    You should fix your indentation too. I thought you're missing curly braces, but it appears you hided them.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!