# Why is my Code not working?

• March 19th, 2014, 10:23 PM
Rhiannon1488
Why is my Code not working?
I was suppose to create a simple Java program for calculating the area of a rectangle (height * width). Then check the
user’s input, and make sure that they enter in a positive integer, and letting them try again if
they enter in a negative number.(I'm not sure how to get them to try again.
I am suppose to use an "if" statements and indeterminate loops to achieve the solution.
The program will have the following requirements:
1.Ask the user to enter in both a height and width (as an integer)
2. If the user enters in a negative number, display an error
3. If the user enters in a negative number, give them another chance to enter in the
correct value
4. Calculate the area and display to the screen once two positive integers have been
entered.
Code Java:

```import java.util.Scanner;   public class RectangleAreaCalc   { public static void main(String[] args) { int length; int width; int area;   Scanner input = new Scanner(System.in);   System.out.println("Please enter the length of the rectangle:"); length = input.nextInt(); if (length < 0) { System.out.println("Incorrect Input, Please enter a positive number for length"); } else System.out.println("Please enter the width of the rectangle:"); width = input.nextInt(); if (width <= 0) { System.out.println("Incorrect Input, Please enter a positive number for width"); } else { area = length * width; System.out.println("The area of a rectangle is:" + area); } } }```
• March 19th, 2014, 10:28 PM
dicdic
Re: Why is my Code not working?
you better use a loop.
are you familiar with loops in java?
• March 20th, 2014, 12:29 PM
Rhiannon1488
Re: Why is my Code not working?
I'm know about loops, but for some reason my programs never run when I try to program loops

--- Update ---

I edited my posts and copy and pasted my code. How would I use a loop?
• March 20th, 2014, 12:37 PM
Norm
Re: Why is my Code not working?
Quote:

for some reason my programs never run
Check for errors from the compiler. Copy the full text of any errors and paste it here.
• March 20th, 2014, 01:21 PM
Rhiannon1488
Re: Why is my Code not working?
Quote:

Originally Posted by Norm
Check for errors from the compiler. Copy the full text of any errors and paste it here.

C:\Users\rhiannon\Desktop\CalculateRectangle.java: 5: error: <identifier> expected
public static void(Strings[] args)
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 11: error: ';' expected
System.out.println ("Please Enter in heigth of rectangle;")
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 12: error: '(' expected
if
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 13: error: ')' expected
height > 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 14: error: ';' expected
System.out.println ("Please Enter a positive number for height:")
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 16: error: not a statement
height < 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 16: error: ';' expected
height < 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 17: error: ';' expected
System.out.println ("Please Enter in width of rectangle;")
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 18: error: '(' expected
if
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 19: error: ')' expected
width > 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 20: error: ';' expected
System.out.println ("Please Enter a positive number for height:")
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 22: error: not a statement
height < 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 22: error: reached end of file while parsing
height < 1
^
C:\Users\rhiannon\Desktop\CalculateRectangle.java: 23: error: reached end of file while parsing
14 errors

Tool completed with exit code 1
• March 20th, 2014, 01:51 PM
Norm
Re: Why is my Code not working?
Can you post the code that is causing the errors in post#5?

I do not get any errors compiling the code in post#1