3

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.

Aliaksandr Belik
  • 12,725
  • 6
  • 64
  • 90
Rocky
  • 45
  • 1
  • 2
  • 8

4 Answers4

4

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

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 5
    You can use String.format(String format, Object... args), but in order to do so, you need to pass an array for the args, not a single object: String.format("hello %s", new Object[]{"world"}); – Jonas Dec 19 '13 at 12:52
0

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);
}
holap
  • 438
  • 2
  • 18
  • 2
    Initially I tried the similar code but during compilation the same error persisted . **Error in method invocation: Static method format( java.lang.String** I have Java 1.6. Why is beanshell interpreter not able to read String.format() method? – Rocky Dec 05 '13 at 12:58
  • @Rocky - I don't really know it's not working. But using String.format("%08d", new Object[]{i}) will work :) – icyerasor Dec 07 '16 at 15:28
0

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.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • It doesn't seems that he wants to loop throw every elements in the array. – holap Dec 05 '13 at 12:46
  • 1
    Yes you guys are correct. I over looked that. The reason I added Long[] was to avoid a error i got earlier **Evaluation Error: Sourced file: sequence.bsh : Error in method invocation: Static method format( java.lang.String, int ) not found in class'java.lang.String' :at Line: 4 : in file: sequence.bsh : String .format ( "%08d" , i )** Now I am getting the same error after adding n.length. I have java 1.6 version. Why is beanshell interpreter not able to read String.format() method? – Rocky Dec 05 '13 at 12:54
0

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.

Jay
  • 1,389
  • 1
  • 10
  • 15