# Unable to correct my errors [code provided]

• September 9th, 2013, 11:13 AM
Milaynie
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?

Code Java:

```  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"); } }   }```
• September 9th, 2013, 11:24 AM
Cornix
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:
Code :

```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.
• September 9th, 2013, 11:39 AM
KevinWorkman
Re: Unable to correct my errors [code provided]
You could also use a Scanner. It really depends on exactly what the assignment calls for.
• September 9th, 2013, 11:42 AM
Milaynie
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
• September 9th, 2013, 12:05 PM
KevinWorkman
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.
• September 9th, 2013, 12:14 PM
Milaynie
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

Code java:

```  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"); } }   }```
• September 9th, 2013, 10:30 PM
Junky
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.
• September 10th, 2013, 06:11 AM
syedbhai
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.