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

Thread: Unable to correct my errors [code provided]

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Unable to correct my errors [code provided]

    Hello everybody, I just started getting a course in Java and have no previous knowledge of the programming language. We have a homework assignment where we have to determine if a given point lies within the bounds of a rectangle based on 4 previous integers (x and y coordinates of point one and two respectively). I've got the "if else" part covered I only don't know how to initialize my variables because those need to be filled in by the program user to get an output of error, inside or outside.
    What am I doing wrong/missing?

    Thank you so much for your help in advance!

     
    public class Rectangle{
     
      public static void main(String args[]){
     
        int a;
        int b; 
        int c;
        int d;
        int e;
        int f;
     
        // length = c - a 
        // width = d - b
     
     
        if ( a <= c && b >= d  ) {  //if point 2 is not to the right and below point one
          System.out.println("error");
        }
        else if ( (b >= e ||e <= d ) && (a >= f || b <= c)) {  //determine if 3rd point is in the bounds 
          System.out.println("inside");
        }
        else {
          System.out.println("outside");
        }
      }
     
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Unable to correct my errors [code provided]

    There are basically 2 functions you need; 1). you have to check the size of the String-array args, you can do this with "args.length" and you need to read a string from the array and cast it into a number, you can do this with the wrapper classes like Integer and Float / Double.
    For example, you could do:
    if (args.length == 6) {
     // Input has correct size, read the values and store them in the variables.
     a = Integer.parseInt(args[0]);
     b = Integer.parseInt(args[1]);
     c = ...
    else {
     // Either set the variables to default values or shut down the program, or whatever.
    }

    On a sidenote: You should really use better names for your variables. If you continue like this (especially if you ask somebody for help) you will have a hard time.
    Better names would be "pointX, pointY, rectangleX1, rectangleX2, rectangleY1, rectangleY2" or something like that.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Unable to correct my errors [code provided]

    You could also use a Scanner. It really depends on exactly what the assignment calls for.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to correct my errors [code provided]

    I also thought of maybe using a Scanner, but without it seemed like it would look less complicated (guess I was wrong,haha)
    Thank you Cornix for your help, you're certainly right about naming my variables property, would make things look more clearly indeed.

    maybe for extra info, this is the assignment :
    Rectangle:
    On input are 6 integers, representing three points in a Cartesian coordinate system, for each point first the x- and then the y-coordinate. The first two points designate a rectangle with sides parallel to the axes. The first point is the top left corner of the rectangle, the second point is the bottom right corner. The program has to decide whether the third point is inside the rectangle (including the edges) or outside. The program should output an error message if the rectangle is ill-defined, i.e., if the second point is not to the right of and below the first point. The program may assume that no other errors occur in the input.

    Remark
    It is required that the output is exactly as described. E.g., the word "outside" should be spelled with lowercase letters, start at the beginning of the line, have no trailing spaces, etc.

    Examples
    Input
    1 4 4 1 2 2

    Output
    inside

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Unable to correct my errors [code provided]

    We've given you two options, and how you proceed is up to you. Try something out and post your updated code if you're confused.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unable to correct my errors [code provided]

    Hi, yes thank you! I've solved it
    Well not entirely, I still have to fix my "if" statement cause it's supposed to give "error" with certain inputs but it doesn't.

    Here is my end code

     
    import java.util.Scanner;
     
    public class Rectangle{
     
      public static void main(String args[]){
     
        Scanner sc = new Scanner(System.in); // enables users to give the input via console
     
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int d = Integer.parseInt(sc.next());
        int e = Integer.parseInt(sc.next());
        int f = Integer.parseInt(sc.next());
     
        if ( c <= a && b >= d  ) {  //if point 2 is not to the right and below point one
          System.out.println("error");
        }
        else if ( (b >= e || e <= d ) && (a >= f || b <= c)) {  //determine if 3rd point is in the bounds 
          System.out.println("inside");
        }
        else {
          System.out.println("outside");
        }
      }
     
    }

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Unable to correct my errors [code provided]

    Print out the values of a b c & d to make sure they contain the values you think they should.
    Improving the world one idiot at a time!

  8. #8
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Unable to correct my errors [code provided]

    Hello.
    First come up with a simple algorithm to solve your problem theoretically. You can do it by trial and error method.
    Once you feel like the algorithm works correctly convert into a simple program.
    Its a good practice to understand the problem thoroughly and come up with algorithm before writing the program.

    Syed.

Similar Threads

  1. Replies: 11
    Last Post: September 9th, 2013, 04:51 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. [SOLVED] No errors, but output not correct? Only one loop performed correctly! Help please :)
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 21st, 2012, 11:18 AM
  4. I still have errors. Can you PLEASE correct my code?!
    By sam30317 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2011, 06:10 AM

Tags for this Thread