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

  • Home
  • SEARCH
  • 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 4082272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:10:01+00:00 2026-05-20T18:10:01+00:00

I currently have an ArrayList which houses the first 1000 prime numbers. I am

  • 0

I currently have an ArrayList which houses the first 1000 prime numbers. I am able to successfully print the list to the console.

I am then applying the following method:

public static ScalesSolution RMHC(ArrayList<Double> weights, int n, int iter){

    private String scasol;

    ScalesSolution sol = new ScalesSolution(n);

    for(int i = 1; i <= iter; i++){

        double oldsol = sol.ScalesFitness(weights);

        sol.smallChange(n);
        sol.println();

        double newsol = sol.ScalesFitness(weights);

        if(newsol > oldsol){
            newsol = oldsol;
        }
    }
    return(sol);
}

Main method:

public static void main(String[] args){

    ArrayList<Double> primes = new ArrayList<Double>();

    primes.addAll(CS2004.ReadNumberFile("1000 Primes.txt"));

    RMHC(primes, 10, 50);

}

ScalesSolution class:

public class ScalesSolution{

public void smallChange(int n)
{
    Random rand = new Random();
    int p = (rand.nextInt(n) - 1);

    //Checks if p < 0.  If so, then p will not have 1 subtracted from it.
    if(p < 0){
        p = (rand.nextInt(n));
    }

    String x = new String();

    x = scasol.substring(0, p);

        if (scasol.charAt(p) == '0')
            scasol.replace('0', '1');
        else if (scasol.charAt(p) == '1')
            scasol.replace('1', '0');
            scasol = x;
}//End smallChange()

}

Whenever I call the method, however, I receive the following error no matter what I enter for the parameters. (FYI, ArrayList<Double> weights is the list of primes, int n is the size of the solution to look for and iter is the number of iterations that the algorithm will run for.)

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
at java.lang.String.substring(Unknown Source)
at ScalesSolution.smallChange(ScalesSolution.java:90)
at Lab8.RMHC(Lab8.java:15)
at Lab8.main(Lab8.java:46)

As mentioned above, the list contains 1000 elements (1000 – 1 indices), however I always receive the error above.

As you can see, it points to an error at index position 6, but there are 1000 - 1 index positions, so why does this happen? The index position changes with each run, but each and every time I run it, the error arises.

Thank you.

  • 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-20T18:10:02+00:00Added an answer on May 20, 2026 at 6:10 pm

    The problem is on this line:

    x = scasol.substring(0, p);
    

    The value of p (6) that you are passing in to the substring method is too large for the string scasol.

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

Sidebar

Related Questions

I currently have an arraylist in C# which holds the my data base table
I currently have an arraylist in which I add blossom objects ( which fall
I have a data structure, for which I am currently using an ArrayList .
I have a program which needs to be able to search through an ArrayList
I currently have an arrayList() called players in JavaScript in which I push data
I currently have an arraylist containing classes in C#. The arraylist is filled like
I have an ArrayList which holds Planes (enemies) on my android game. These planes
I currently have a tabview with 3 tabs. 1 tab loads an ActivityGroup which
In an application I'm currently working on I have my first activity with a
List<String> flowers = new ArrayList<String>(); My for loop currently looks like this... for (int

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.