1

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.

jax
  • 37,735
  • 57
  • 182
  • 278
  • 3
    You misunderstand our questions. We know the Java API you want to use accepts variable parameters. What we are saying is: Take a step back and look at the bigger picture of what you are trying to accomplish. Why do you have to use **THAT** Java API? Why can't you either (1) shift more of the work over to RPG, or (2) shift more of the work over to Java? Clearly you don't like or are not willing to consider (1), so then why can't you do (2)? – John Y Jul 05 '11 at 14:58
  • I use the sprintf() C library function in RPGLE to do that same thing. Why is Java needed to do it? – user2338816 Mar 16 '16 at 06:53

2 Answers2

4

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.

David G
  • 3,940
  • 1
  • 22
  • 30
2

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
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
  • yes, because I need to pass this to a PDF creation program and it needs a more powerful formatter than RPG has – jax Jun 29 '11 at 02:39
  • @jax: What "more powerful" formatting features do you need? If you can make the fully formed string in RPG, it simplifies the parameter passing into Java (you can just pass the finished string instead of a variable number of pieces of data). – John Y Jun 29 '11 at 19:28
  • The java PDF class will do the formatting for me. I just need a way to pass the formatted string with variable arguments for flexibility. – jax Jun 30 '11 at 22:38
  • 1
    @jax: You are using the word "format" in too many ways. To be clear, a *formatted* string has already had formatting applied to it. This is why I called it a *finished* string. You have not given us a reason why you can't just pass the *finished* string to Java. Alternately, what is it that you need RPG to do for you? If you are so unhappy with RPG formatting, why can't you do the whole thing in Java? The main thing I'm trying to get at is actually that you still haven't told us enough about the context here. – John Y Jul 01 '11 at 02:30
  • While it's possible to use RPG as a controller to call all Java, why would you do that? The ability to call Java adds the ability to call a PDF library or the POE library. Stuff that isn't available in RPG. Tell us which PDF library you are calling, maybe some example code. We just don't understand what you are trying to do. – Mike Wills Jul 01 '11 at 13:39
  • I am trying to call a Java method with a varargs parameter. I wrote the API myself based on the gnupdf library. It looks like this in java `public void myMethod(String format, String... args)`. In java `String...` this means that a variable number of String arguments can be passed to the method. I wan't to know how to prototype this type of construct in RPGLE. – jax Jul 04 '11 at 10:26