Search code examples
javaarraysnetbeansruntime-errorexit-code

netbeans java code exits with exit code 1 with no apparant reason


the first class is this

/*
 * To change this license header, choose License Headers in Project  Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package logical.conundrum;
import java.io.*;
import java.util.Scanner;
/**
*
* @author Tuvanen Kiruitsu
*/
public class LogicalConundrum {
   /**
    * @param args the command line arguments
    */
    public static int [] RNG_OUT;
    public static String[][] inv;
    public static float hp;
    public static int[] Room;
    public static int RNG_SET; 
    public static void main(String[] args) {
        inv = new String[5][5];
        System.out.print("How many numbers do you want > ");
        Scanner input = new Scanner(System.in);
        int sc_in = input.nextInt();
        int loop_1 = sc_in;
        int sc_out = sc_in++;
        RNG_SET = sc_in++;
        RNG_OUT = new int[sc_out];
        RNG_OUT[0] = 0;
        RNG.RNG(); /fails here

        for (int a = loop_1; a >= 1; a--){
            System.out.println (RNG_OUT[a]); 
        }
    }
}

and the second one that the first one references

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package logical.conundrum;

/**
*
* @author Tuvanen Kiruitsu
*/
public class RNG {

    /**
     *
     */
        static int rng[];
        static void RNG(){
            int RNG_SETUP = LogicalConundrum.RNG_SET;
            rng = new int[4];
            for (int c = RNG_SETUP; c >= 1; c--){
                simulateRNG();
                LogicalConundrum.RNG_OUT[c] = rng[2]; //fails here
        }
    }
    static int simulateRNG(){
        byte y = 1;
        tickRNG(y);
        y--;
        tickRNG(y);
        return rng[2];
    }
    static void tickRNG(byte y) {
        rng[0] = (byte) (5 * rng[0] + 1);
        boolean c = (rng[1] & 0x80) != 0;
        boolean z = (rng[1] & 0x10) != 0;
        rng[1] = (byte) (2 * rng[1]);
        if (c == z){
            rng[1]++;
        }
        rng[2+y] = (byte) (rng[1] ^ rng[1]);
    }
}

and regardless of what i put in it always returns this

run:

How many numbers do you want > 10

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

at logical.conundrum.RNG.RNG(RNG.java:23)

at logical.conundrum.LogicalConundrum.main(LogicalConundrum.java:32)

C:\Users\user\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1

BUILD FAILED (total time: 4 seconds)

the code always exits like this I don't know what whent wrong don't know how to fix it any and all help is appreciated


Solution

  • According to your input 10 your array will be size of 10. In RNG.RNG() loop you want to start from index 11 what causes out of bounds exception. Change this line:

    int RNG_SETUP = LogicalConundrum.RNG_SET;
    

    to

    int RNG_SETUP = LogicalConundrum.RNG_OUT.length - 1;
    

    Also there is a problem inside the last loop again index out of bounds. Change this:

    for (int a = loop_1; a >= 1; a--){
    

    to:

    for (int a = RNG_OUT.length - 1; a >= 1; a--){
    

    If you are looping over array then you should always use length of array as the limit instead of some variable. If you intend in your code to loop over entire array then you should change your limits inside of loops to x >= 0.