Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:57:32+00:00 2026-05-25T14:57:32+00:00

When I declare an array in java I get this error when running: Exception

  • 0

When I declare an array in java I get this error when running: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException. Despite that the variable totalNumbers has a value. It’s working when I replace that variable into a number like 5. Must it be a number when declare array?

int randomNumbers[];
randomNumbers = new int[totalNumbers];

Added some more code, but’s the variable names and comments are in swedish! But perhaps the code could be understodd despite of that!? Or why not learn some swedish!= 🙂

// deklarera arrays för tal under 500 och för tal över 500
int slumptalMindre[];
slumptalMindre = new int[antalSlumptalMindreÄn500];

int slumptalStörre[];
slumptalStörre = new int[antalSlumptal - antalSlumptalMindreÄn500];

//gå genom första array och omplacera tal till ny array
for(int x = 0; x < antalSlumptal; x++) {
    if(slumptal[x] < 500) {

        slumptalMindre[x] = slumptal[x];
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:57:33+00:00Added an answer on May 25, 2026 at 2:57 pm

    You did not post what values are possible for antalSlumptalMindreÄn500 and antalSlumptal, but according to your code I assume both are positive and antalSlumptal > antalSlumptalMindreÄn500.

    I.e. let antalSlumptal = 20 and antalSlumptalMindreÄn500 = 5.

    Then the length of array slumptalMindre is 5 and the length of slumptalStörre is 15. But there is no declaration of array slumptal.

    In your for loop variable x ranges from 0 to 19 and in each iteration you access array slumptalMindre with index x. Apparently this leads to an ArrayIndexOutOfBoundsException when the x-value becomes 5.

    I can only guess what your intention was. It seems that you want to apply some filter on array slumptal and put all values less than 500 in another array.

    One problem in using an array for the filter result is, that you don’t know the length of the result after the loop is finished. But you have to initialize the array beforehand.

    So one solution could be the usage of two loops. The first iterates through the array and counts how many numbers are less than 500. Then you initialize your array with the right size and the second loop iterates over the array again and copies the result values to the result array. In the second loop you have to be carefull to distinguish between the index to access array slumptal and the index to access the shorter result array:

    // count the numbers less than 500
    int count = 0;
    for(int x = 0; x < slumptal.length; x++) {
      if(slumptal[x] < 500)
        count++;
    }
    slumptalMindre = new int[count];
    
    int y = 0; // Index to access array slumptalMindre
    for(int x = 0; x < slumptal.length; x++) {
      if(slumptal[x] < 500)
        slumptalMindre[y++] = slumptal[x]; 
    }    
    

    But the solution above is not optimal because you have to iterate through the array twice. This only because the result should be a fixed length array. It is easier to use a dynamically sized data structure like a List in this case:

    // Using a ArrayList of Integer values as the result
    List<Integer> slumptalMindre = new ArrayList<Integer>();
    
    for(int x = 0; x < slumptal.length; x++) {
      if(slumptal[x] < 500)
        slumptalMindre.add(slumptal[x]); 
    }    
    

    One more hint about coding style: It is valid to use Umlauts in variable and class names in Java. But doing so is considered as bad style because you can easily run into problems. The Java compiler uses the platform default encoding for the source files unless you specify the -encoding option. On Windows the default encoding is Cp1252, on Linux usually UTF-8. Things went worse if Umlauts are part of class names because this leads to class file names containing Umlauts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a Java style question. The brackets empty declare an array reference. With
I know I can declare an array of string in XAML like this: <x:Array
In languages like C++ and Java, it is possible to declare an array of
In my h file I declare a var that later should be an array:
I need to declare an array of pointers to functions like so: extern void
I was wondering if it is possible to declare an array (size not known
In Matlab, I recall being able to declare an array and initialize it and
suppose I declare a dynamic array like int *dynArray = new int [1]; which
I declare a static char array, then I pass it to a function. How
I declare a variable for a 64 bit counter as : long long call_count;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.