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

Thread: Why is my Code not working?

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

    Default 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.
    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);
    	}
    }
    }
    Attached Files Attached Files
    Last edited by Rhiannon1488; March 20th, 2014 at 12:32 PM.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Why is my Code not working?

    you better use a loop.
    are you familiar with loops in java?
    and please just paste your code here instead of attaching a text file. so other could read it easily

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why is my Code not working?

    for some reason my programs never run
    Check for errors from the compiler. Copy the full text of any errors and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why is my Code not working?

    Quote Originally Posted by Norm View Post
    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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 18th, 2014, 02:56 AM
  2. Is my code working
    By return0 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 29th, 2013, 08:48 PM
  3. Code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 23rd, 2013, 02:12 PM
  4. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  5. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM