Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: My code-segment somehow executes again after I press RETURN, which isn't what I want

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Question My program repeats certain displays after ENTER is pressed, as well as with loops

    The program I am trying to make should have a menu to select from that looks like this:

    Geometry Calculator
    1. Calculate the Area of a Circle
    2. Calculate the Area of a Rectangle
    3. Calculate the Area of a Triangle
    4. Quit
     
     
    Enter your choice (1-4):

    So, as you can see, the program is supposed to do math-calculations for a shape's surface-area. The
    problem is that I can't get it to stop repeating the same question after it is finished. The code for the
    program is below under the filenames AreaGiver.java (the main-program) and Geometry.java (the class
    objects are made from for the main program). The java-comments will explain what I thought I was doing right, as well as the errors I realized in bold, parenthesized, capital-letters.

    // This import-statement will be for Scanner-class input and output.
    import java.util.Scanner;
     
    // This import-statement will be for DecimalFormat-class numerical-setups.
    import java.text.DecimalFormat;
     
    public class AreaGiver
    {
         public static void main(String[] args)
         {
              // Here is a variable-setup for the these matched-up reasons:
              int areaChoice;  // to hold the user's calculation-selection,
              double radius,   // the radius of a circle,
                   length,        // the length of a rectangle,
                   width,         // the width of the rectangle,
                   base,          // the base of a triangle,
                   height,        // the height of the triangle,
                   area;          // the area of each shape,
     
              // to create a Scanner-class object named keys,
              Scanner keys = new Scanner(System.in);
     
              // and a DecimalFormat-class setup for the math-work results.
              DecimalFormat numSetup = new DecimalFormat("#,##0.00");
     
              // Here, a menu is displayed by these statements, so the
              // numerical-choice can be made by the user.
              System.out.println("Geometry Calculator");
              System.out.println("1. Calculate the Area of a Circle");
              System.out.println("2. Calculate the Area of a Rectangle");
              System.out.println("3. Calculate the Area of a Triangle");
              System.out.println("4. Quit");
              System.out.println();
              System.out.println();
              System.out.println("Enter your choice (1-4):");
              areaChoice = keys.nextInt();
     
              // This loop should execute each calculation-process based on the 
              // selection while the areaChoice-variable is outside the range of 1-4.
              // ([B]THE IF-STATEMENTS WILL REPEAT IF YOU PRESS ENTER.[/B])
              do
              {
                   // This if statement will make the selection.
                   if (areaChoice == 1)
                   {
                        // Get the circle's radius.
                        System.out.println("What is the radius of your circle?");
                        radius = keys.nextDouble();
     
                        // Store the area in the area-variable for easier-reading.
                        area = Geometry.getCircularArea(radius);
     
                        // Display the circle's area.
                        System.out.println("Your circle's area is: " 
                                      + numSetup.format(area));
                   }
     
                   else if (areaChoice == 2)
                   {
                        // Get the rectangle's length.
                        System.out.println("What is the length of your rectangle?");
                        length = keys.nextDouble();
     
                        // Get the rectangle's width.
                        System.out.println("What is the width of your rectangle?");
                        width = keys.nextDouble();
     
                        // Store the area in the area-variable for easier-reading.
                        area = Geometry.getRectangularArea(length, width);
     
                        // Show the area of the rectangle.
                        System.out.println("Your rectangle's area is: "
                                      + numSetup.format(area));
                   }
     
                   else if (areaChoice == 3)
                   {
                        // Get the triangle's base.
                        System.out.println("What is the base of your triangle?");
                        base = keys.nextDouble();
     
                        // Get the triangle's height.
                        System.out.println("What is the height of your triangle?");
                        height = keys.nextDouble();
     
                        // Put the area in the area-variable for easier-reading.
                        area = Geometry.getTriangularArea(base, height);
     
                        // Show the triangle's area.
                        System.out.println("Your triangle's area is: "
                                      + numSetup.format(area));
                   }
     
                   else if (areaChoice == 4)
                   {
                        // Show that the program ends here.
                        System.out.println("Program ending");
                   }
     
                   else
                   {
                        // Show an error-message to get a number within the range of 1-4.
                        System.out.println("Enter a number within the range of"
                                            + " 1-4.");
     
                        areaChoice = keys.nextInt();
                   }
     
              } while (areaChoice <= 0 && areaChoice >= 5);  // Here, make the choice the user made.
         }
    }

    // Use this Scanner-class import-statement for output.
    import java.util.Scanner;
     
    public class Geometry
    {
         // The getCircularArea-method should calculate the area of a circle
         // and give an error-message to get the right number-type.
         public static double getCircularArea(double radius)
         {
              // Create a Scanner-class object for output.
              Scanner keys = new Scanner(System.in);
     
              // Give an error-message for a value of 0 or below.
              // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B])
              while (radius <= 0)
              {
                   System.out.println("Do not enter a value of 0 or below.");
              }
     
              // Return the circle's area.
              return Math.PI * Math.pow(radius, 2.0);
         }
     
         // The getRectangularArea-method should calculate the area of a rectangle
         // and give an error-message to get the right number-type.
         public static double getRectangularArea(double length, double width)
         {
              // Create a Scanner-class object for output.
              Scanner keys = new Scanner(System.in);
     
              // Give an error-message for a value of 0 or below.
              // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B])
              while (length <= 0 && width <= 0)
              {
                   System.out.println("Do not enter values of zero or below.");
              }
     
              // Return the rectangular-area.
              return length * width;
         }
     
         // The getTriangularArea-method should calculate the area of a triangle
         // and give an error-message to get the right number-type.
         public static double getTriangularArea(double base, double height)
         {
              // Create a Scanner-class object for output.
              Scanner keys = new Scanner(System.in);
     
              // Give an error-message for a value of 0 or below.
              // ([B]THIS LOOP WILL KEEP REPEATING UNLESS YOU PRESS CTRL + C.[/B])
              while (base <= 0 && height <= 0)
              {
                   System.out.println("Do not enter values of zero or below.");
              }
     
              // Return the triangular-area.
              return base * height * 0.5;      
         }
    }

    If there is anything erroneous about the posting's presentation, let me know. (I'm new to this.)
    Last edited by copeg; July 6th, 2011 at 06:23 PM. Reason: Removed text from merged threads


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: My code-segment somehow executes again after I press RETURN, which isn't what I w

    Please do not duplicate threads. I have merged your threads (somehow the merge went a bit too far and merged 3 posts into one, I edited your post above to avoid confusion - sorry about that).

    Check the while condition
    while (areaChoice <= 0 && areaChoice >= 5);
    a value cannot be <= 0 and >= 5

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: My code-segment somehow executes again after I press RETURN, which isn't what I w

    SOG, here. Thank you for your help. Now I just need to understand why the do-while loop won't repeat after a correct error-message response.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: My code-segment somehow executes again after I press RETURN, which isn't what I w

    Two things I noticed.

    1.) You never actually told it

    System.exit(0); for your ending code, so it won't actually ever end.

    2.) If you put that menu to pop up inside the do while loop, then it might repeat if given a bad answer. In fact, it'll repeat if given a good answer too. As is, there's no way for the user to enter another value unless it's inside the loop.

    3.) while (base <= 0 && height <= 0)
    {
    System.out.println("Do not enter values of zero or below.");
    }

    Assuming you don't want a base OR a height to be less than or equal to 0, you should change that && to ||

    Otherwise it will only go through the loop if BOTH are less than or equal to 0.
    Last edited by javapenguin; July 6th, 2011 at 09:44 PM.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    SOG (July 7th, 2011)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: My code-segment somehow executes again after I press RETURN, which isn't what I w

    Quote Originally Posted by javapenguin View Post

    1.) You never actually told it

    System.exit(0); for your ending code, so it won't actually ever end.
    There is absolutely no reason the code should make a call to exit.

  7. The Following User Says Thank You to copeg For This Useful Post:

    SOG (July 7th, 2011)

  8. #6
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: My code-segment somehow executes again after I press RETURN, which isn't what I w

    This thread is now solved. I was supposed to add an input-getting statement after my error-messages in Geometry.java and make a while-loop before the if-else-if statement in AreaGiver.java. All I needed were some of these responses.
    Last edited by SOG; July 7th, 2011 at 09:25 AM. Reason: Forgot to include 2 things.

Similar Threads

  1. Replies: 2
    Last Post: October 29th, 2010, 03:14 AM
  2. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  3. press a button, get textfield content
    By chopficaro in forum AWT / Java Swing
    Replies: 1
    Last Post: May 2nd, 2010, 12:28 PM
  4. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 09:23 AM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM

Tags for this Thread