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.

3
  • show us where is scasol declaration Commented Mar 22, 2011 at 18:50
  • 1
    It's worth noting that your scasol.replace() methods aren't doing anything, since they return a new string which you aren't doing anything with. Also, your indentation suggests you believe the scasol = x line is part of the else block, but it isn't. Best practice here is to always uses braces, even when they're optional. Commented Mar 22, 2011 at 19:08
  • @dty Thank you for your comment. I see what you mean - I too suspected that the scasol.replace() methods weren't doing anything but I was not sure how to go about it. What I am aiming to do is to make a small change (hence the method name) to a String variable so that when a 1 is encountered, it is changed to a 0 and vice versa. May I ask what you would recommend? Commented Mar 22, 2011 at 20:29

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You are getting an exception because p is not a valid index for the String scasol. Can you print out that String and check its value? Is it the expected value? Also, no need to do new String() as strings in Java are immutable.

Comments

0

This line:

at ScalesSolution.smallChange(ScalesSolution.java:90)

point you that in 90 line in ScalesSolution you have Exception, so try before this line call System.out.println with scasol and p value, then you'll see what cause the problem

Comments

0

In addition to GregInYEG's answer, you can apply modulus on p to avoid this problem like this: int p = (rand.nextInt(n) - 1) % scasol.length();

Comments

0

It may be that the issue occurs because you shorted scasol on each call of smallChange.

The lines

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;

are functionally equivalent to

scasol = scasol.substring(0, p);

and thus cut your string scasol is shortened to p chars and may not be long enough for the second call within your for loop.

I think these lines should actually do something different? Can you please describe what the expected function of this method should be?

Also the lines

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));
}

look strange. What do you want to accomplish here? What it does is get a random number between 0 and n-1 while n-1 is much more seldom than any other value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.