• Register
1 vote
16 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

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 92 views
92 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 25 views
25 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 mphil 2.3k points
0 votes
1 answer 19 views
1 vote
1 answer 38 views
38 views
Problem: I am a begginer in java programming language and I have been coding for just some days. To practice my programming I wrote this code in java: public class Main { public static void main(String[] argv){ int 2count, h-l, a/b, } } But It shows me the error message ... , . expected Syntax error on token ",", ;expected at Main.main(Main.java:3) Whats is wrong in my code can anyone help me out?
asked Apr 6 LizzyM 6.1k points
1 vote
1 answer 24 views
24 views
Problem: I got error while applying weblogic patch useing bsu.sh: patch_download_dir=$FMW_HOME/utils/bsu/cache_dir -patchlist=GFWX -prod_dir=$FMW_HOME/wlserver_10.3 -log=$FMW_HOME/utils/bsu/bsu_weblogic.log Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded ... .bea.plateng.patch.Patch.main(Patch.java:281) Can anyone explain me this and help me to solve this error?
asked Mar 27 LizzyM 6.1k points