-1

Hello!
I'm doing a thing to where the java program will let the user input a length, and width of a triangle. In return it will show the length, width, and the area. I think I have everything correct here, however, the public voids I inserted does not work... Any help on fixing this?

Thanks

import javax.swing.JOptionPane;
import java.util.Scanner;

public class AreaRectangle
{
   public static void main(String[] args)
   {
      double length;    // The rectangle's length
      double width;     // The rectangle's width
      double area;      // The rectangle's area
   	String input;
		String output;
		String STR1 = "Please enter the length:  ";
		String STR2 = "Please enter the width:  ";
		String STR3 = "Your area of the triangle is:  ";
		
		
		
		
		
      // Get the rectangle's length from the user.
		public void getLength(double length)
		{
		
		Scanner keyboard = new Scanner(System.in);//enables keyboard
		System.out.println(STR1);//outputs STR1
		length = keyboard.nextDouble();//input gets saved as value

      length = getLength();
   	}
      // Get the rectangle's width from the user.
		public void getWidth(double width)
		{
		
		Scanner keyboard = new Scanner(System.in);//enables keyboard
		System.out.println(STR2);//outputs STR2
		width = keyboard.nextDouble();//input gets saved as value

      width = getWidth();
		}
      // Get the rectangle's area.
		public void getArea(double area)
		{
		
      area = getArea(length, width);
		{
		
      // Display the rectangle data.
      System.out.println(length, width, area);
   }
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by cale.macdonald
0

You cannot create a method inside the switch statement. I would move one of the methods out and change its name (perhaps getValueFromUser).
That method would initialize your scanner object and return the value entered by the user. In the switch statement print the question, then call the method initializing what you wanted.

for example:

import javax.swing.JOptionPane;
import java.util.Scanner;
 
public class AreaRectangle
{
	public static void main(String[] args)
    {
		...
		// Get the rectangle's length from the user.
	              System.out.println(STR1);//outputs STR1
		length = AreaRectangle.getValueFromUser();
		...
	}
   
	public static double getValueFromUser()
	{
		Scanner keyboard = new Scanner(System.in);//enables keyboard
		return keyboard.nextDouble();
	}
}

However you should approach this using proper Java objects. Use the main method for your testing purposes instead of providing all the heavy lifting (calculations)

Edited by cale.macdonald: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.