> Ok, first time here. My problem is simply this:
>
> I have a two dimensional array and I need to count
> the distance between two points in an array. Please
> help. For the love of God!
Can you explain in detail what that means? The points in an array are just numbers, how do you define the distance? Maybe check out this: http://en.wikipedia.org/wiki/Distance_formula
Well, I have a 2D array. Its really simple. Beginners stuff. I get the user to input the values(integers) into the array. I then give them an option to measure the distance between two of the points that they have already enetered. But I'm having problems with the actual process of counting the distance.
> Well, I have a 2D array. Its really simple. Beginners
> stuff. I get the user to input the values(integers)
> into the array. I then give them an option to measure
> the distance between two of the points that they have
> already enetered. But I'm having problems with the
> actual process of counting the distance.
That doesn't really define what you mean by "distance" though. Are you talking about treating the array indices like cartesian coordinates? If so , then just implment the distance formula for two points, the position of the first elment, and the position of the second element.
Well I'm back. Sorry about this. Here is my code, there is a problem:
double distance = Math.sqrt((xtwo - xone)(2) + (yone - ytwo)(2));
Two errors, both pointing towards the subtraction symbols. It reckons a class is required, and that it found a value. (I'm using Textpad)
I also got a worrying comment, saying: "The system is out of resources. Consult the following stack trace for details.
java.lang.OutOfMemoryError
I suppose this is the best way of doing it. Notice how the sort method is the only one I've failed to account for:
public class Mapping {
public static void main(String[] args) {
System.out.println("");
//Declaration of Array.
int[][] answer;
answer = new int [3][3];
//Calls the main menu method and essentially starts program.
menu(answer);
}
//Menu Method.
public static void menu(int array[][]) {
//Declaration of menu-choice string.
String menuchoice = new String();
//Output display of main menu.
System.out.println("");
System.out.println("NB: TYPE THE CORRECT NUMBER OPPOSITE THE CHOICE.");
System.out.println("");
System.out.println("__MENU__");
System.out.println("");
System.out.println("1: Initialise all co-ordinates to zero.");
System.out.println("2: Manually enter co-ordinates into grid.");
System.out.println("3: Search for specific value.");
System.out.println("4: Randomise Co-ordinates.");
System.out.println("5: Sort Co-Ordinates.");
System.out.println("6: Display Current co-ordinates.");
System.out.println("7: Measure the distance between two points.");
System.out.println("8: End Program.");
System.out.println("");
System.out.print("Option choice: ");
//Input of choice for menu.
menuchoice = Keyboard.readString();
System.out.println("");
// If statement for menu navigation.
if ((menuchoice).equals("1")) {
zero(array);
}
else if ((menuchoice).equals("2")) {
input(array);
}
else if ((menuchoice).equals("3")) {
search(array);
}
else if ((menuchoice).equals("4")) {
random(array);
}
else if ((menuchoice).equals("5")) {
}
else if ((menuchoice).equals("6")) {
display(array);
}
else if ((menuchoice).equals("7")) {
measure(array);
}
else if ((menuchoice).equals("8")) {
}
else {
System.out.println("");
System.out.println("*** Error, type number opposite choice! ***");
System.out.println("");
menu(array);
}
}
//Measurement Method.
public static void measure(int arraydistance[][]) {
System.out.println("");
System.out.println("__DISTANCE");
System.out.println("");
System.out.print("Please enter the longitude of the first point: ");
double xone;
xone = Keyboard.readDouble();
System.out.println("");
System.out.print("Please enter the latitude of the first point: ");
double xtwo;
xtwo = Keyboard.readDouble();
System.out.println("");
System.out.print("Please enter the longitude of the second point: ");
double yone;
yone = Keyboard.readDouble();
System.out.println("");
System.out.print("Please enter the latitude of the second point: ");
double ytwo;
ytwo = Keyboard.readDouble();
System.out.println("");
double distance = ((xtwo-xone)*(xtwo-xone))+((yone-ytwo)*(yone-ytwo));
Math.sqrt(distance);
System.out.println("RESULT");
System.out.println("");
System.out.println("The distance between the points is " + distance + ".");
System.out.println("");
System.out.println("__OPTIONS");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Find distance between two other values.");
System.out.println("");
System.out.print("Option choice: ");
//Declaration of String for use in option menu of method.
String distancechoice = new String();
//Input.
distancechoice = Keyboard.readString();
//If statement for methods sub menu.
if ((distancechoice).equals("1")) {
menu(arraydistance);
}
else if ((distancechoice).equals("2")) {
measure(arraydistance);
}
else{
System.out.println("");
System.out.println("***Incorrect Input***");
System.out.println("");
measure(arraydistance);
}
}
//Display Array Method.
public static int[][] display(int arraydisplay[][]) {
//Output of methods title.
System.out.println("");
System.out.println("DISPLAY__CO-ORDINATES_");
System.out.println("");
//For Loop to display array.
for (int i = 0; i < arraydisplay.length; i++) {
for (int j = 0; j < arraydisplay[i].length; j++) {
System.out.print(arraydisplay[i][j] + "\t");
}
}
//Methods end option.
String displaychoice = new String();
System.out.println("");
System.out.println("");
System.out.println("__OPTIONS");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Display Co-ordinates again.");
System.out.println("");
System.out.print("Option choice: ");
displaychoice = Keyboard.readString();
System.out.println("");
//If statement for methods end option input.
if((displaychoice).equals("1")) {
menu(arraydisplay);
}
else if((displaychoice).equals("2")) {
display(arraydisplay);
}
return null;
}
//Random method.
public static void random(int arrayrandom[][]) {
//Declaration of Doubles for Random process.
double a;
double mix = 0;
double p = 0;
System.out.println("");
System.out.println("__RANDOMISE__");
System.out.println("");
System.out.print("Please enter any number: ");
a = Keyboard.readDouble();
System.out.println("");
//For Loop for Random process.
for(int i = 0; i < arrayrandom.length; i++) {
for (int j = 0; j < arrayrandom[i].length; j++) {
// Double P changes for each value and is then involved in the randomisation process.
p = p + 23;
// Randomise process.
mix = (Math.random() + 12 * 5 - 2 + p - a);
arrayrandom[i][j] = (int)mix;
//Further Randomise process using If Statement.
if (arrayrandom[i][j] <= 3 && arrayrandom[i][j] >= 53) {
arrayrandom[i][j] = arrayrandom[i][j] + 12 - 33 * 6;
}
else if (arrayrandom[i][j] <= 60 && arrayrandom[i][j] >= 55) {
arrayrandom[i][j] = arrayrandom[i][j] + 23 / 2 + 6;
}
else if (arrayrandom[i][j] >= 61 && arrayrandom[i][j] <= 65) {
arrayrandom[i][j] = arrayrandom[i][j] + 11 * 2;
}
else if (arrayrandom[i][j] >= 70 && arrayrandom[i][j] <= 250) {
arrayrandom[i][j] = arrayrandom[i][j] * 2 + 12 / 2;
}
else if (arrayrandom[i][j] <= 0 && arrayrandom[i][j] >= -121) {
arrayrandom[i][j] = arrayrandom[i][j] * 2 + 21;
}
}
}
//Declaration of String for Methods end option.
String randomchoice = new String();
//Output of Method.
System.out.println("");
System.out.println("All values have now been randomised.");
System.out.println("");
System.out.println("__OPTIONS_");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Repeat section.");
System.out.println("");
System.out.print("Option choice: ");
//Input for method option.
randomchoice = Keyboard.readString();
System.out.println("");
//If Statement for methods option.
if((randomchoice).equals("1")) {
menu(arrayrandom);
}
//Repeats Method; this time making the randomised array random.
else if((randomchoice).equals("2")) {
random(arrayrandom);
}
//Repeats Method.
else {
System.out.println("");
System.out.println("***Incorrect Input***");
System.out.println("");
random(arrayrandom);
}
//Calls back menu while sending result of this method back through.
menu(arrayrandom);
}
//Search method.
public static int[][] search(int arraysearch[][]) {
//Declared variables, output of methods title.
int num;
int p = 0;
System.out.println("");
System.out.println("SEARCH_");
System.out.println("");
System.out.print("Enter the value you are looking for: ");
//Input
num = Keyboard.readInt();
System.out.println("");
System.out.println("SEARCHRESULT__");
//For Loop that searches array for inputted value.
for (int a = 0; a < arraysearch.length; a++) {
for (int b = 0; b < arraysearch[a].length; b++) {
if (arraysearch[a][b] == num){
System.out.println(num + " is present at: " + a + "," + b);
p++;
}
}
}
//String for methods end option menu.
String searchchoice = new String();
//Output of methods end option menu.
System.out.println("");
System.out.println("The co-ordinate appears " + p + " time(s) throughout the co-ordinates.");
System.out.println("");
System.out.println("__OPTIONS");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Search for another value.");
System.out.println("");
System.out.print("Option choice: ");
//Input
searchchoice = Keyboard.readString();
//If statement for methods sub menu.
if ((searchchoice).equals("1")) {
menu(arraysearch);
}
else if ((searchchoice).equals("2")) {
search(arraysearch);
}
else{
System.out.println("");
System.out.println("***Incorrect Input***");
System.out.println("");
search(arraysearch);
}
return null;
}
//Initialise array to zero method.
public static int[][] zero(int arrayzero[][]) {
System.out.println("");
for (int a = 0; a < arrayzero.length; a++){
for (int b = 0; b < arrayzero[a].length; b++){
arrayzero[a][b] = 0;
}
}
String zerochoice = new String();
System.out.println("_ZERO__");
System.out.println("");
System.out.println("All values have now been set to zero.");
System.out.println("");
System.out.println("__OPTIONS_");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Repeat section.");
System.out.println("");
System.out.print("Option choice: ");
zerochoice = Keyboard.readString();
System.out.println("");
if((zerochoice).equals("1")) {
menu(arrayzero);
}
else if((zerochoice).equals("2")) {
zero(arrayzero);
}
else {
System.out.println("");
System.out.println("***Incorrect Input***");
System.out.println("");
zero(arrayzero);
}
return null;
}
//Manual Array Input Method.
public static int[][] input(int arrayinput[][]) {
System.out.println("");
System.out.println("MANUAL_INPUT");
System.out.println("");
for (int c = 0; c < arrayinput.length; c++){
for (int d = 0; d < arrayinput[c].length; d++){
System.out.print(c + "," + d + ": ");
arrayinput[c][d] = Keyboard.readInt();
}
}
System.out.println("");
String inputchoice = new String();
System.out.println("");
System.out.println("__OPTIONS_");
System.out.println("1: Finish and return to menu.");
System.out.println("2: Repeat input.");
System.out.println("");
System.out.print("Option choice: ");
inputchoice = Keyboard.readString();
System.out.println("");
if((inputchoice).equals("1")) {
menu(arrayinput);
}
else if ((inputchoice).equals("2")) {
input(arrayinput);
}
else {
System.out.println("");
System.out.println("***Incorrect Input***");
System.out.println("");
input(arrayinput);
}
return arrayinput;
}
}
Compiled ok. But when run:
Exception in thread "main" java.lang.ClassCastException
at java.util.Arrays.mergeSort(Arrays.java:1152)
at java.util.Arrays.sort(Arrays.java:1079)
at Mapping.sort(Mapping.java:158)
at Mapping.menu(Mapping.java:118)
at Mapping.main(Mapping.java:26)
//Sort Method.
public static void sort(int arraysort[][]){
Arrays.sort(arraysort);
}
Arrays.sort only takes 1d arrays. Sorting a list is easy, because there's only one dimension. Sorting 2 dimensions is harder because you have to decide how to sort the second dimension. Are you going to sort columns, but not sort rows? Sort rows but not columns? That's why you can't just say "sort a 2d array", you have to choose how to sort it.
> Sort rows!
Excellent! Then all you need to do is loop over each row, and sort it using the tools you already have available. Hopefully you implemented your arrays like this [row][col] instead of like this [col][row]. If so, then pass theArray[row] to the sort method. If not, it's a little trickier, but still totally doable.
Bingo! THANK YOU! Now, for my last one, and probably the easiest one you've heard in a while:
I'm displaying the array in another method, but it won't display in the 3x3 format I want it to. Heres the code.
public static int[][] display(int arraydisplay[][]) {
//Output of methods title.
System.out.println("");
System.out.println("DISPLAY__COORDINATES_");
System.out.println("");
//For Loop to display array.
for (int i = 0; i < arraydisplay.length; i++) {
for (int j = 0; j < arraydisplay[i].length; j++) {
System.out.print(arraydisplay[i][j] + "\t");
}
}