Reputation: 103
I want to insert Coordinate x and y into both dimensional arrays and print it out. For the example given to the given code when it asks for input it doesn't do it once but twice then accepts the value. I don't if the values are actually getting stored in each array or just one. Is it possible to show output independently of the matrix? I aprecciate any help or corrections! Bless you. Here's the code:
import java.util.Scanner;
public class MummyMadness {
public static void main(String []args){
int [][] desert = new int [1][1];
int x =0;
int y =0;
int mummies[] = new int [1];
int Mlimit = 100000;
coordinates(desert,mummies,x,y);
}
public static int[] inputmummy(int mummies[], int Mlimit){
int i;
Scanner input = new Scanner(System.in);
System.out.println("How many mummies you want?");
System.out.println("Note that the limit of mummies is 100k");
i = input.nextInt();
if(mummies[i] > Mlimit)
{
System.out.println("You exceeded from the mummies!");
}
return mummies;
}
public static int[][] coordinates(int desert[][],int mummies[], int x, int y){
//Idea use length of mummies for coordinates but if you take out desert i doesnt work
System.out.println("Enter mummy coordinates");
for(int i = 0; i < desert.length; i++){
System.out.println("Enter x:");
Scanner input = new Scanner(System.in);
if(input.nextInt() > desert.length)
{
System.out.println("Error x exceeded desert length");
break;
}
desert[i][0] = input.nextInt();
System.out.println("Mummy location x: " + desert[i][0]);
for(int j = 0; j < mummies.length; j++){
System.out.println("Enter y:");
Scanner input2 = new Scanner(System.in);
if(input.nextInt() > desert[0].length)
{
System.out.println("Error y exceeded desert length");
break;
}
desert[0][j] = input2.nextInt();
System.out.println("Mummy location y: " + desert[0][j]);
System.out.println(desert[i][j])
}
}
return desert;
}
}
Upvotes: 1
Views: 84
Reputation: 2310
You are using input.nextInt twice() - once for checking and once for actually inserting into array. Everytime you call this function, it expects an input. Hence its asking for input twice. Do this instead:
int tmp = input.nextInt();
if(tmp > desert.length)
{
System.out.println("Error x exceeded desert length");
break;
}
desert[i][0] = tmp;
Upvotes: 1