muhkah's picture

how to print one string object without set newLine() manually

Hi all,
On my web application, I wanna use TextPrinter (http://www.java4less.com/textprinter/index.html) for printing.
Anybody know, How do I print one string object with multiple line inside, let's say the string name is out like this:

AID : XXXXXX
A/C Number : XXXXXXXXX
A/C Name & Address : REKENING ANTARA SHB DGN TOP UP

Is it possible for using some function to put it once? Without I must set it manually:

printer.printString ("AID                     :   000922");
 
printer.newLine();
 
printer.printString ("A/C Number              :   XXXXXXXXX");
 
printer.newLine();

just put it like this maybe, printer.printString(out);

but it doesn't work, just first line of report could be printed. any idea?

Really thanks for the response.

Regards,

Kahlil


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hortus Longus's picture

Hi.

The simplest way should be to put the strings in a list and let them show in a small routine like:

class printStringwithNewLine(list sl)
{
  foreach(string s in sl)
        {
            printer.printString(s);
            printer.newLine();
        }
}
muhkah's picture

Hi Longus, what a great solution, really thanks...
I already tried it with little bit modification in java :d

ArrayList outList = new ArrayList();
String perOutString = null;
StringTokenizer tokens = new StringTokenizer(outString,"\n");
while (tokens.hasMoreTokens()) { // for converting String to List
       outList.add(tokens.nextElement());
}
for (Iterator outIte = outList.iterator(); outIte.hasNext();) {
      perOutString = (String) outIte.next();
      log.info(perOutString);
      printer.printString(perOutString,prop);
      printer.newLine();
}

Once again, thanks.

Regards,
Kahlil

martinsm's picture

Your code is really strange and ineffective. Here is more effective code:

string[] lines = outString.Split('\n');
foreach (var s in lines)
{
      printer.printString(s);
      printer.newLine();
}

or in java:

String[] lines = outString.split("\n");
for (String s : lines)
{
      log.info(s);
      printer.printString(s);
      printer.newLine(); 
}
muhkah's picture

Hi martin,
ups...I think my code is too inefective, I completely agree with you...
I already tried with the simpler one that you suggest it's worked great.

Thanks