0

Beginner here. I'm having problems running this series of for loops to find which integers are missing from an array.

public class FunWithArrays{
    public static void main(String[] args){
        String nString = args[0];
        int n = Integer.parseInt(nString);
        int inputArray [] = {1,2,4};
        System.out.println(" The missing numbers are " );
        findMissingNum(n, inputArray);
    }
    public static void findMissingNum(int n, int[] inputArray){
        for (int i = 1; i <= inputArray.length; i++){
            int count = 0;
            for( int j = 0; j < n; j++){
                if(inputArray[j] == i){
                    count ++;
                }
                if (count == 0){
                    System.out.println(i);
                }
            }
        }   
    }
}

I get the answer I want, namely 3, however it doesn't print but rather shows up in a runtime error:

java.lang.ArrayIndexOutOfBoundsException: 3
at FunWithArrays.findMissingNum(FunWithArrays.java:17)
at FunWithArrays.main(FunWithArrays.java:9)

the method should take an input n from the user (when the program is run) as the largest value of the array and print all the ones missing The logic is the outer for loop should traverse the array for numbers 1-n, and the inner loop should add to the count variable each time it finds a certain number. At the end of iteration it should print any numbers with a final "count" of 0. THIS IS LITERALLY DRIVING ME CRAZY!!! thanks in advance :)

dumbPotato21
  • 5,353
  • 5
  • 19
  • 31
  • Please format your code. It's literally very difficult to read. – shmosel May 22 '17 at 23:31
  • Your problem lies here: `int i = 1; i <= inputArray.length; i++)` when ´i = inputArray.length´ any try to access inputArray[i] will give you ArrayIndexOutOfBoundsException – Jorge Campos May 22 '17 at 23:38
  • @JorgeCampos There is no such access attempt. The problem is the assumption that `n <= inputArray.length`. – shmosel May 22 '17 at 23:38
  • ops... didn't look the rest of the code xD my bad... – Jorge Campos May 22 '17 at 23:39
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Ousmane D. May 22 '17 at 23:44
  • Apologies about the format...this is my first post and the tab keyboard shortcut was giving me difficulties. Thanks for all you help! – Miranda Green May 23 '17 at 15:48

3 Answers3

1
  1. First of all, you should traverse from 0 to (inputArray.length-1) index of inputArray. This will get rid of the ArrayIndexOutOfBoundsException, because java array indexing starts from 0 not 1.

  2. And for inner loop, run from 0 to n, since n is the max number.

  3. And Thirdly, it should be inputArray[i] == j, not inputArray[j] == i, same for printing the value. In you case I believe you have n>=4, so it was trying to access inputArray[3] via inputArray[j] call. That's why you are getting this out of bound error.

zafar_sust_bd
  • 101
  • 1
  • 12
  • You should traverse `0` to `inputArray.length`, unless there is a reason to not care about the last element. String[5]; length = 5; elements 0...4; looping 0 to < length gives elements 0...4. – KevinO May 22 '17 at 23:51
  • if you are accessing 0-4 it means you are traversing 0 to length-1. Using * – zafar_sust_bd May 23 '17 at 00:56
  • Perhaps we are saying the same thing, but the loop should be `for (int i =0; i < inputArray.length; ++i)`. And I may have read your statement incorrectly, but it at least implied that the loop would be to `inputArray.length - 1` (which has been incorrectly suggested multiple times in answers). – KevinO May 23 '17 at 00:58
  • In the question, the code snippet used `<=` operator in the first loop. That's why people are suggesting `length -1`. Anyway, **Cheers!** – zafar_sust_bd May 23 '17 at 01:02
  • when I make the changes suggested I get the following output: 'java FunWithArrays 4 The missing numbers are 0 0 1 0 1 2 3' so it works...sort of? except that 0 is not supposed to be in the range, and I only need the final number printed. Honestly having a bit of trouble following. Thanks so much! – Miranda Green May 23 '17 at 15:54
  • Also I think the idea is I'm trying to use the index i as a representation of the integers from 1-n themselves...hence why I started at 1 and not 0 – Miranda Green May 23 '17 at 16:05
  • if you are trying to use i as a representation of `1-n` (which you should) then do not put the condition `i <= inputArray.length`. It will be `i<=n` and `j < inputArray.length`. – zafar_sust_bd May 25 '17 at 21:36
0
  1. The i incrementing to <= ipnutArray.length is not causing the error because i is never used as the index. What is causing the error is when n > length.

  2. Also, you should not be checking n elements starting from the beginning because n is the max value, not the number of elements.

L1ghtShadow
  • 102
  • 10
0

I think your code means like this: nest loop always run through inner loop first before run the outer loop.

   public static void findMissingNum(int n, int[] inputArray){
        for (int i = 1; i <= n; i++){
            int count = 0;
            for( int j = 0; j < inputArray.length; j++){
                if(inputArray[j] == i){
                    count ++;
                }
                if (count == 0){
                    System.out.println(i);
                }
            }
        }   
    }

I will just use a while loop instead:

int num =1;
while(num<=n){
    for(int i = 0;i<inputArray.length;i++){
        if(inputArray[i]!=num){
            System.out.println(num);
        }
    }
    num++;
}