0

So assuming I am printing out a huge record of 500 staff members. And upon printing out the record I would like it to look something like:

   1. Matthew J. $USD 28.404
   2. Donna M. $USD 43.254
   3. Jordan D. $USD 15.532
   4.
   5. 
   6.
   7. and so on......

However...with the normal printout command it looks like:

System.out.println();

My output :

   Matthew J. $USD 28.404
   Donna M. $USD 43.254
   Jordan D. $USD 15.532



   and so on......

EDIT: So how do I get the output to have each line numbered?

LATER EDIT:

My System.out.print is printing out info out of a list (from a JSON, all that is solved thx to Prasad Khode) here is my code:

for (Manager iterator : managersList) {

    System.out.println(iterator.getName() + " - " + iterator.getSalary());
}

This prints out

Name - Salary

How can I make it print:

1. Name - Salary
2. Name - Salary

and so on. A solution will be so much appreciated. Thanks.

3 Answers3

2
int i = 1;
while(something) {
    println(i + "." + SomeName);
    i++
}

Is this what you are looking for?

0

If u are using a for loop to print the records then you can use

System.out.println((i+1)+". "+ record[i]);
Sulabh Jain
  • 342
  • 1
  • 9
0

Modifying System.out.println() so that it outputs line numbers is not an easy task. You could create a new PrintStream subclass (thats what out is), override all the appropriate methods in PrintStream so that println(boolean), and println(char) and println(long) and so on all print out the line number at the start (which is maintained in the class itself). Then, create it pointing to System.out and somehow reattach System.out to this PrintStream. Its not impossible, and could be a worthy undertaking for someone wanting to mess around in the guts of System, but there are a number of easier ways to do this.

First off, consider just printing the entire stream out and then pipe it through awk or some such program. Much easier and doesn't mess around with things.

Assuming you do want to get into the "this needs to be the output from Java", lets just create a wrapper class.

package com.michaelt.so.outwrapper;

import java.io.PrintStream;

public class Wrapper {
    static PrintStream out = System.out;
    static int lineNum = 1;

    static void println(String arg) {
        out.println((lineNum++) + ". " + arg);
    }

    static void printlnFancy(String arg) {
        out.printf("%3d. %s\n", lineNum++, arg);
    }
}

and then use that:

package com.michaelt.so.outwrapper;

public class Main {
    public static void main(String[] args) {
        for(char c = 'a'; c <= 'f'; c++) {
            Wrapper.println(""+c); // I just want a string
        }

        for(char c = 'A'; c <= 'F'; c++) {
            Wrapper.printlnFancy(""+c);
        }
    }
}

Which prints out:

1. a
2. b
3. c
4. d
5. e
6. f
  7. A
  8. B
  9. C
 10. D
 11. E
 12. F

I'm not saying this is a good thing. And it involves changing all your System.out.println() to the Wrapper class... but it doesn't actually mess with System.out which could lead to surprising things.

Community
  • 1
  • 1
  • I solved my issue with @RobinGenolet 's solution . However you took a lot of time and effort in posting this very elaborate answer so thank you for that. – LogitechJuice Mar 15 '15 at 18:22
  • Amazing advice. I actually created the Wrapper class in another project just to check how it works. However it takes less time using the above approved solution. And in the words of the greatest Benjamin Franklin "Time is money." . I truly appreciate the time and effort you put in writing this answer. Thanks again. – LogitechJuice Mar 15 '15 at 19:10