Question

I am new to Beanshell scripting. I am trying to generate sequential numbers, the scripting code I tried is as below

File Name: sequence.bsh

string = new String();
Long[] n = new Long[] {1000};
for (i=0; i < n; i++){
    sequence = String.format("%08d", i);
    System.out.println(sequence);
}

When I try to run this code I get the below Error:

Evaluation Error: Sourced file: sequence.bsh : Operator: '"<"' inappropriate for objects : at Line: 3 : in file: sequence.bsh : ;

The above lines of code work as expected in a compiled java program & I get sequence generated from 00000001 to 00009999.

I need to know how to rectify this operator error & assign the result to a variable so that I can use it inside a JMeter test case. something like vars.put("VARNAME", i.toString());

Thanks in advance.

Was it helpful?

Solution

Beanshell is not very Java, I guess you need to use DecimalFormat class instead of String.format() method.

import java.text.DecimalFormat;

DecimalFormat df = new DecimalFormat( "00000000" );

int  n = 1000;

for (int i=0;i<n;i++)
{
    String sequence = df.format(i);
    System.out.println(sequence);
}

There is a nice Beanshell scripting guide that can help a lot

OTHER TIPS

Change

for (i=0; i < n; i++){

to

for (i=0; i < n.length; i++){

You're attempting to compare an integer, i, and an array of type Long.

n is an array, you shouldn't use < with an array. But why do you use an array? You don't need it for the purpose of the code you posted. You could try to change it into a Long.

string = new String();
Long n = 1000;
for (i=0; i < n; i++){
    sequence = String.format("%08d", i);
    System.out.println(i);
}

Also you can use JMeter function: __counter that generates a new number each time it is called, starting with 1 and increasing incrementally by one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top