• Register
1 vote
22 views

Problem :

I have very recently started to learn programming in Java. I am trying to execute my java code but I am facing the below error.

Exception in thread "main" java.lang.arrayindexoutofboundsexception:”

I am unable to understand above error.

Please find below my code:

public static int[] myoddSort ( int myinput[] )
{
    int myamountOfOdd = 0;
    int j = 0;

    for(int i = 0; i < myinput.length; i++)
    {
        if (myinput[i] % 2 != 0)
            myamountOfOdd++;
    }

    int[] myodd = new int[myamountOfOdd];

    for(int i = 0; i <= 199; i++)
    {
        if (myinput[i] % 2 != 0)
            /* This line is not working, according to my debugger*/
            myodd[j] = input[i];
            j++;
    }
    return myodd;
}

Please help me in fixing above error.

8 5 2
3,230 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution :

I found your error very familiar so I am trying to help you.

I can certainly tell you the reason behind your exception is you forgot to put the brackets around a body of the if condition. Your If condition should be like below code :

for(int i = 0; i <= 199; i++)
{
    if (myinput[i] % 2 != 0){        
        myodd[j] = myinput[i];
        j++;
    }
}

I hope my above code will help you in fixing your error.

8 4
5,680 points

Related questions

0 votes
1 answer 102 views
102 views
Problem : I am a beginner to Java programming. While executing my program i got below error exception in thread "main" java.lang.arrayindexoutofboundsexception: 10
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 9 views
9 views
Problem: public class dereks { public static void main (String [] args){ int array [] = {1,2,3,5,6,7}; int sum =0; for(int counter=0; counter<=array.length; counter++){ sum+=array[counter]; } System.out.println(sum); } } Can anyone tell ... I can't understand why I'm getting the error message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at dereks.main(dereks.java:7)" .
asked Feb 20 Ali 5.1k points
0 votes
1 answer 19 views
19 views
Problem: I am a beginner to programming. I'm a few weeks into my first programming class, so please bear with me. I am not a person to ask for help, so I have searched for an answer extensively with no luck. This is also my first time posting ... correct for future posts. This is what I got while running the program. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:50 on line 23
asked Feb 20 Ali 5.1k points
0 votes
1 answer 17 views
17 views
Prblem: The error ArrayIndexOutOfBoundsException: 1 means index 1 is invalid and it's out of bounding i.e. more than the length of the array. Since the array has a zero-based index in Java, this means you are trying to access the second element of an array which only contains one element.
asked Feb 19 Ali 5.1k points
0 votes
1 answer 4 views
4 views
Problem: I am a beginner to programming. I'm a few weeks into my first programming class, so please bear with me. I am not a person to ask for help, so I have searched for an answer extensively with no luck. This is also my first time posting anything in any type of forum, so if my question structure is off I'm sorry and I will correct for future posts.
asked Feb 18 Ali 5.1k points
0 votes
1 answer 13 views
0 votes
1 answer 30 views
0 votes
1 answer 11 views
11 views
Thank you in advance for any help?
asked Jan 5 TeamScript 13.5k points
0 votes
1 answer 46 views
46 views
Problem : Currently I am learning Java. I have written code in Java but it gives me below error &ldquo;Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Minesweeper.main(Minesweeper.java:5)&rdquo;. I am unable to understand above error. How can I fix above error.
asked Feb 22, 2020 mphil 2.3k points
0 votes
1 answer 22 views
22 views
I wrote some code in java: public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter size of array:"); int size = sc.nextInt(); int[] intArray = new int[size]; for ( ... .journaldev.exceptions.ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:23) What is the problem in my code and why am I getting this kind of error?
asked Mar 14, 2020 LizzyM 6.1k points