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 3 of 3

Thread: [Solved] Problem with Area Calculator Code!

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    New Zealand
    Posts
    8
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Talking [Solved] Problem with Area Calculator Code!

    Hi there.

    I am beginning to learn Java and have decided to create an area calculator. But I am having a big problem.
    import java.util.Scanner;
    import static java.lang.System.out;
    import java.lang.Math;
     
    public class AreaCalculator 
    {
    	public static void main(String[] args) 
    	{
    		Scanner keyboard = new Scanner(System.in);
    		String shape;
    		double width, height, radius, base, area;
    		boolean wantToExit = false, exit = false;
     
    		out.println("Welcome to the Area Calculator!");
    		out.println("Please enter all data without a measurement!\n");
     
    		while(exit == false)
    		{
    			out.print("Please enter the shape: ");
    			shape = keyboard.nextLine();
     
    			if(shape.equals("square") || shape.equals("rectangle"))
    			{
    				out.print("Please enter the width: ");
    				width = keyboard.nextDouble();
    				out.print("Please enter the height: ");
    				height = keyboard.nextDouble();
     
    				area = width * height;		
    				out.println("\nThe area is: " + area);
    			}
    			else if(shape.equals("triangle"))
    			{
    				out.print("Please enter the base size: ");
    				base = keyboard.nextDouble();
    				out.print("Please enter the height: ");
    				height = keyboard.nextDouble();
     
    				area = (base / 2) * height;
    				out.println("\nThe area is: " + area);
    			}
    			else if(shape.equals("circle"))
    			{
    				out.print("Please enter the radius (half of diameter): ");
    				radius = keyboard.nextDouble();
     
    				area = Math.PI * Math.sqrt(radius);
    				out.println("\nThe area is: " + area);
    			}
    			else
    			{
    				out.println("Sorry, we do not currently support that shape!");
    			}
     
    			out.print("Do you want to exit: (true/false) ");
    			wantToExit = keyboard.nextBoolean();
     
    			if(wantToExit == true)
    			{
    				out.print("\nOkay, goodbye!");
    				exit = true;
    			}
    			else
    			{
    				exit = false;
    			}
    		}
     
    		keyboard.close();
    	}
    }

    When I run this and choose true to exit the loop. The message "Sorry, we do not currently support that shape!" appears.

    Why is this and how can I fix this?

    Regards, Bradley.
    Last edited by tazeunite00; August 24th, 2014 at 04:50 AM. Reason: problem was solved


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    411
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Problem with Area Calculator Code!

    Generally I would not advise using Boolean types as input variables.
    They should be used for comparisons. To answer your question, change
    the type that reads the exit command to a String perhaps? Then ensure the
    text entered matches the command to exit the loop.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (August 24th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Location
    New Zealand
    Posts
    8
    My Mood
    Amazed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Area Calculator Code!

    Hi.

    Thanks for your help, will give it a try now!

    Regards, Bradley.

    --- Update ---

    Hi.

    I updated my code and it is working perfectly now. Thanks for the help.

    Regards, Bradley.

Similar Threads

  1. Incredibly new at java... trying to make area calculator
    By shain in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2014, 04:56 PM
  2. Calling a Method with my Hexagon Area Calculator
    By EarlyBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2013, 08:25 PM
  3. Replies: 5
    Last Post: August 10th, 2013, 04:21 PM
  4. Slight problem with fraction calculator code
    By Jampolo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2013, 05:01 PM
  5. Area calculator.
    By LoganC in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 22nd, 2012, 08:15 PM

Tags for this Thread