# Thread: Help with printing out new coordinates

1. Member
Join Date
May 2010
Posts
1
Rep Power
0

## Help with printing out new coordinates

Hi there im new to programming and have a problem with some code

import java.util.Scanner;

public class StageOne
{
public static void main(String[] args)
{
final int WIDTH = 100; // width of the sheet in pixels
final int HEIGHT = 100; // height of the sheet in pixels

int x; // the x-coordinate
int y; // the y-coordinate
String direction; // the direction chosen

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// 1. Ask the user for the ant's x-coordinate
System.out.print("Enter your ant's x-coordinate: ");
x = keyboard.nextInt();

// 2. If the x-coordinate is outside the allowed range,
// print an error message and exit
if(x < 0 || x > WIDTH-1)
{
System.out.println("The x-coordinate must be between 0 and " + (WIDTH-1));
System.exit(-1);
}

// 3. Ask the user for the ant's y-coordinate
System.out.print("Enter your ant's y-coordinate: ");
y = keyboard.nextInt();

// 4. If the integer is outside the allowed range,
// print an error message and exit
if (y < 0 || y > HEIGHT-1)
{
System.out.println("The y-coordinate must be between 0 and "+ (WIDTH-1));
System.exit(-1);
}

// 5. Ask the user for the direction
System.out.println("Which direction would you like to move? Possible Directions are U, UR, R, DR, D, DL, L, UL");
direction = keyboard.next();

// 6. Increase or reduce the x and y-coordinates depending on the direction
if(direction.equals("U"))
{
y += 1;
}
// else if(direction.equals("UR")) ... etc

else if (direction.equals("UR"))
{
y += 1;
x += 1;
}

else if (direction.equals("R"))
{
x += 1;
}

else if (direction.equals("DR"))
{
y -= 1;
x += 1;
}

else if (direction.equals("D"))
{
y -= 1;
}

else if (direction.equals("DL"))
{
y -= 1;
x -= 1;
}

else if (direction.equals("L"))
{
x -= 1;
}

else if (direction.equals("UL"))
{
y += 1;
x -= 1;
}
// 7. If the direction is not one of the allowed values,
// print an error message and exit

else System.out.println("The direction must be either U, UR, R, DR, D, DL, L or UL ");
System.exit(-1);

// 8. "Wrap" the x and y-coordinates if the ant has walked over the edge of the paper
if(x < 0)
{
x += WIDTH;
}
// else if(x > WIDTH-1) ... etc

else if (x > WIDTH-1)
{
x += WIDTH-99;
}

else if (y < 0)
{
y += HEIGHT;
}

else if (y > HEIGHT-1)
{
y += 1;
}

// 9. Print out the new x and y-coordinates

System.out.println(" X coordinate is "+(int)x);
System.out.println(" Y coordinate is "+(int)y);

}
}

when i run the program it goes blank after asking for the coordinates and direction, can anybody see what it is that im doing wrong?
thanks

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
5
Please use code tags and proper indentation when you post your code, as it's much easier to read for those that want to help you. Another suggestion is to break down your program into separate methods instead of putting everything in the main method, as it's a lot simpler to track down a problem if you have small pieces to look over instead of one big blob. My suggestion is to look over the logic of your if else statements, as that is the most likely culprit.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•