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

Thread: Incredibly new at java... trying to make area calculator

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Incredibly new at java... trying to make area calculator

    Hello,

    Like the title says I'm very, very new at java. I started with finding the area of a triangle, but now I'm trying to ask a user what kind of shape they want the area for, then ask questions to get the area. I can't figure out how to take the shape a person types to go to a certain case. It also says shape hasn't been initialized. I don't know how to do that. I've tried googling, but I really can't figure it out. Thanks for the help in advance.

    EDIT: Please ignore lack of calculations in cases... unless that's the problem

    import java.util.Scanner;
    public class TriangleArea {
     
    	static Scanner sc = new Scanner(System.in);
    	public static void main(String[] args){
    	char shape;
    	String text = "Do you want to find the area of a triangle, square, rectangle, or trapezoid?";		
    	System.out.print("Text");
    	switch(shape){
    	case '1':
    		double base;
    		double height;
    		System.out.print("The base of the triangle is: ");
    		base = sc.nextDouble();
    		System.out.print("The height of the triangle is: ");
    		height = sc.nextDouble();
    		double Area1 = base * height;
    		double Area2 = Area1 / 2;
    		System.out.println("The area of the triangle is: " + Area2);
    	case '2':
    		double base1;
    		double height1;
    		System.out.print("The base of the square is: ");
    		base1 = sc.nextDouble();
    		System.out.print("The height of the square is: ");
    		height1 = sc.nextDouble();
    		double Area = base1 * height1;
    		System.out.println("The area of the square is: ");
    	case '3':
    		double base2;
    		double height2;
    		System.out.print("The base of the rectangle is: ");
    		base2 = sc.nextDouble();
    		System.out.print("The height of the rectangle is:");
    		height2 = sc.nextDouble();
    		System.out.println("The area of the rectangle is:");
    	case '4':
    		double base3;
    		double base4;
    		double height3;
    		System.out.print("One side of the trapezoid is: ");
    		base3 = sc.nextDouble();
    		System.out.print("Another side of the trapezoid is: ");
    		base4 = sc.nextDouble();
    		System.out.print("The height of the trapezoid is: ");
    		height3 = sc.nextDouble();
    		System.out.println("The area of the trapezois is: ");
    	default :
    		System.out.println("Invalid shape");
    	}
     
    	}
    }
    Last edited by shain; May 19th, 2014 at 03:37 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,509
    My Mood
    Mellow
    Thanks
    153
    Thanked 613 Times in 600 Posts

    Default Re: Incredibly new at java... trying to make area calculator

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    This is how you initialize a variable:

    j = 5;
    thisString = "initial value";
    thisObject = null;

    As for getting the user's chosen shape, the program needs to make use of the Scanner object you've created to get the user's input. The steps are:

    Give the user the choices ( a menu? )
    Get the user's input ( menu choice ).

    Getting the user's input with your Scanner object looks something like:

    shape = sc.next().charAt( 0 );

    Which could be accomplished in a couple different ways.

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

    shain (May 19th, 2014)

Similar Threads

  1. how do I add Action Listener to my code to make the calculator work
    By alex1994 in forum What's Wrong With My Code?
    Replies: 58
    Last Post: March 8th, 2014, 08:32 AM
  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, 07:25 PM
  3. Replies: 5
    Last Post: August 10th, 2013, 03:21 PM
  4. Replies: 3
    Last Post: January 15th, 2013, 01:18 PM
  5. Area calculator.
    By LoganC in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 22nd, 2012, 07:15 PM

Tags for this Thread