Reputation: 38643
I would like to interface RPGLE with String.format which takes variable length arguments or an array, I also want to pass numbers as well as strings, so I will be using format like "hello %s, you are %d years old"
. Can someone give me some advide on how to prototype this in RPGLE?
UPDATE
It seems that some people were confused with the quesion. To make things clear, I want to prototype the following in RPGLE. Note that the second argument to the method is a varargs
parameter, so any number of arguments can be supplied! RPGLE definitely does not support this, but it does support *nopass
so this my be helpful in achieving the result I need.
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
or
String.format(format, "FirstName", "Init.", "LastName");
I am not interested in how I can format strings in RPGLE, I want to prototype a java method.
Upvotes: 2
Views: 2190
Reputation: 1
I totally agree that we prefer using native RPGLE functions whenever possible. However, as RPGLE developers, we're also open to utilising any external functions as needed.
Here's an example that might be relevant to your situation, though I'm not sure if it directly addresses your case:
Step 1. Create a Java class with a method that accepts a format string and a varargs parameter:
public class StringFormatter {
public static String formatString(String format, Object... args) {
return String.format(format, args);
}
}
Step 2. In the RPGLE program, define the prototype for the Java method. You'll need to use the *JAVA and *NOPASS options to handle the variable arguments.This prototype allows you to pass up to 10 arguments, which should be enough for most cases. If you need more, you can extend the prototype with additional arguments. And finally you can call the formatString method from your RPGLE program like below
dcl-pr formatString varchar(32767) extproc(*JAVA : 'StringFormatter' : 'formatString');
format varchar(32767);
arg1 varchar(32767) options(*nopass);
arg2 varchar(32767) options(*nopass);
arg3 varchar(32767) options(*nopass);
arg4 varchar(32767) options(*nopass);
arg5 varchar(32767) options(*nopass);
arg6 varchar(32767) options(*nopass);
arg7 varchar(32767) options(*nopass);
arg8 varchar(32767) options(*nopass);
arg9 varchar(32767) options(*nopass);
arg10 varchar(32767) options(*nopass);
end-pr;
dcl-s result varchar(32767);
result = formatString('|%1$-10s|%2$-10s|%3$-20s|\n'
: 'FirstName'
: 'Init.'
: 'LastName');
dsply result;
*inlr = *on;
Upvotes: 0
Reputation: 4014
How about using message's to do the formatting ... they are quite powerful and the text is externalized (and CCSID aware).
You can use the QMHRTVM API to retrieve the formatted message.
When you're in RPG code, it's always faster to invoke native functionality than Java.
Upvotes: 4
Reputation: 21275
Is there a particular reason you want to use Java? The overhead of starting up a JVM can be killer on many applications. RPG itself can do that easily using concatenation.
/free
resultString = 'hello ' + %trim(name) + ', you are ' +
%trim(%char(years)) + ' years old';
/end-free
Upvotes: 2