简体   繁体   中英

Dont know why I have this error Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0

This is my code

import java.util.Scanner;
public class Input {
    static class General_Inputs{
        static int Num_Of_Ppes;
        static int Num_Of_States;
        static int Num_Of_Analysis_Years;
        static int Number_Decision_Variables;
        static int Num_objectives;
        static int Num_Constraints;
        static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables];  
    }
public static double[][] Get_Inputs(){
    Scanner State_Vector=new Scanner(System.in);
    System.out.println("Enter the number of Decision_Variables");
    General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
    for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
        System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
        General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
    }
    State_Vector.close();
    return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
    double[][] input=Input.Get_Inputs();

The inputs is like this: Enter the number of Decision_Variables 3 Enter the number of Alternatives for Decision Variable 1 2 This should go three times but it stop at the first decision variable and give the following error I am not sure where is the problem. I understand that this error means the size of the array "Num_Alt_Decision_variable" is zero but why is this I defined this array above, I really tried to search a lot but couldn't find the reason Any help is highly appreciated thanks in advance.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Input.Get_Inputs(Input.java:48)
    at Input.main(Input.java:66)
    static int Number_Decision_Variables; // this is 0 here
    static int Num_objectives;
    static int Num_Constraints;

    // and it's still 0 here.
    static int[] Num_Alt_Decision_variable=new int[Number_Decision_Variables]; 

Number_Decision_Variables is 0 at the time you declare Num_Alt_Decision_variable. So your array is size 0 which is a non-usable array. I suggest that you initialize it with a non-0 number.

Initialize your array after this line:

General_Inputs.Number_Decision_Variables=State_Vector.nextInt();

After you get a realistic value for the Number_Decision_Variables variable.

As an aside, you will want to learn and use Java naming conventions . Variable names should all begin with a lower letter while class names with an upper case letter.

This is the answer I could solve it

import java.util.Scanner;
public class Input {
    static class General_Inputs{
        static int Num_Of_Ppes;
        static int Num_Of_States;
        static int Num_Of_Analysis_Years;
        static int Number_Decision_Variables;
        static int Num_objectives;
        static int Num_Constraints;
        static int[] Num_Alt_Decision_variable;  
    }
public static double[][] Get_Inputs(){
    Scanner State_Vector=new Scanner(System.in);
    System.out.println("Enter the number of Decision_Variables");
    General_Inputs.Number_Decision_Variables=State_Vector.nextInt();
General_Inputs.Num_Alt_Decision_variable=new int[General_Inputs.Number_Decision_Variables]
    for(int Num=0;Num<General_Inputs.Number_Decision_Variables;Num++){
        System.out.println("Enter the number of Alternatives for Decision Variable "+(Num+1));
        General_Inputs.Num_Alt_Decision_variable[Num]=State_Vector.nextInt();
    }
    State_Vector.close();
    return Current_Cond_State_Cof_lngth;
}
public static void main(String args[]){
    double[][] input=Input.Get_Inputs();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM