Millie Millie - 2 months ago 6
Java Question

2d Array Loop Count is not working properly

So this is pretty close to complete, here is the
Output. My methods are working perfectly, so that's not the issue. The output will give the best explanation but basically I have to print a list of 4096 random integers and ask the users input for a number. With that number I have to tell the user how many loops it took to find it. The issue is that it is not counting properly. It skips numbers that are in the output and if it is not found it prints both of the if statements. I have already built this program for a 1d array, and I am unable to use an Arraylist. Thanks!

Scannner s = new Scanner(System.in);

int[][] input = new int[5][1];
int[][] arrayone = new int[4097][1];
int loop = 0;

for (int id = 0; id < input.length; id++) {
for (int x = 0; x <input[id].length; x++) {
System.out.println("Please enter a number between " + min + " and " + max);
input[id][x] = s.nextInt();

if (min <= input[id][x] && input[id][x] <= max) {
for (int count = 0; count < arrayone.length; count++) {
for (int count2 = 0; count2 < arrayone[count].length; count2++) {
if (arrayone[count][count2] != input[id][x]) {
loop++;
}
else {
break;
}
}
}
if (input[id][x] != arrayone.length){
System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
}
if(loop > 4096) {
System.out.println(input[id][x] + " was not found");
}
loop = 0;
}
}


Update:

I have used part of the code below to update my own and now the print is either not found or 4096, here is an Updated Output. Below is the changes I have made to the code according to suggestion:

if(loop > 4096) {
System.out.println(input[id][x] + " was not found");
}
else{
System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
}
loop = 0;

Answer

As mentioned in the comments, your code has some oddities that need addressing, especially the dimensions. This is disguising the fact you aren't breaking out of the outer loop to end the search process with your break statement, as an unlabeled break only breaks out of the innermost loop. Assuming you create a 2D array that has more than the one dimension, you can use a label to break out of the outer loop.

outerloop:
for (int count = 0; count < arrayone.length; count++) {
    for (int count2 = 0; count2 < arrayone[count].length; count2++) {
        loop++;    //assume first check counts as one loop
        if (arrayone[count][count2] == input[id][x]) {
            break outerloop;
        }
    }
}

For your second issue, you might need to correct the numbers as you fix the oddities in your program's code, but since your program only has two results (found or didn't find number), you can use a simple if/else statement to write only one output dialogue per search.

    if(loop > 4096) {
        System.out.println(input[id][x] + " was not found");
    }
    else{
        System.out.println("It took " + loop + " time(s) to find the number " + input[id][x]);
    }
Comments