
- Java program to find area of rectangle using scanner how to#
- Java program to find area of rectangle using scanner code#
Shape = String.valueOf(rectangle.calculateArea()) I tried a simple Scanner input for now like this: Scanner keyboard = new Scanner(System.in)
Java program to find area of rectangle using scanner how to#
I already created setters and getters for the different shapes and tested that everything works, what I struggle with is how to implement the Scanner to accept the different Shape inputs. I now need it to accept user input to choose one of the three shapes, for it to accept user input for the parameters (length and width) and for it to then calculate it and after printing the correct result, to close the program. My program at the moment consists of a method called: calculateArea() which is used to calculate 3 different shapes area (a Rectangle, Circle and a Square). 'm currently in the process of learning Java and I have been given a task to re-write my program. Public static double getArea(double length, double width)
Java program to find area of rectangle using scanner code#
Causes any code underneath the return statement to be ignoredĬombining all of that should allow the compiler errors to stop, and make your program work correctly. Return width // Returns the value to your main function To fix the compiler error, a return statement needs to be added in to the helper functions, such as: Scanner keyboard = new Scanner( System.in ) ĭouble width = keyboard.nextDouble() // Sets the value to return to your main function If that value will never be sent, the program will most likely be unable to compile - an error will be thrown stating something along the lines of "expected type double" when you try to call that function. Therefore, when you set length to equal getLength(), what you're trying to do is set the value of your local length variable to equal the value coming back from your helper function. In the case of a void function, such as main() or displayData(), the method states that it will not return a variable of any specific type. The other thing to consider is that when a method has a return value, such as a double in the case of getLength(), getWidth(), and getArea(), the piece of code calling the function is expecting some variable of that type to be returned. While passing the Scanner to each function separately would allow your methods to work as intended, the first option is slightly more readable. Initialized within the class, but outside of any methods Scanner keyboard = new Scanner( System.in ) Since your program is broken up into several methods, the data inside each method is local unless you store it inside the class itself.įor example, your helper functions for getLength() and getWidth() wouldn't be able to access your keyboard Scanner unless you declared it outside of the main method, as such: import What am I screwing up on and how would I go about fixing it? I am a beginner in programming so please bear with me :D. Public static void displayData(double length, double width, double area)

Scanner keyboard = new Scanner(System.in) Īrea = getArea(double length, double width) I don't know how to complete this code because right now what I have is this: import



