Search code examples
javafor-looparraylistconstructorenumerator

I want to print out the enumerator data in columns or as a list


SO...I have this piece of code here.

    class secondary
{
    enum DocDetails
    {
        DrHowlett(500, "Orthopedist"),
        DrMurdock(300, "ENT specialist"),
        DrNarendra(1000, "Nephrologist"),
        DrWhite(10000, "Pulmonologist"),
        DrSummers(1500, "Opthalmologist"),
        DrStark(12000, "Cardiologist"),
        DrBanner(20000, "Radiologist");

    private int x;
    private String z;
    private DocDetails(int y, String a)
    {

        x=y;
        z=a;
    }

}
    public static void main(String args[])
    {
        for(DocDetails d:DocDetails.values())
        {
            System.out.println(d);
            System.out.println(d.x);
            System.out.println(d.z);
        }
    }
}

And I want to print out like this

DrHowlett 500 Ortopedist
DrMurdock 300 ENT specialist
....and so on. 

But this is the actual output of the code

DrHowlett
500
Orthopedist
DrMurdock
300
ENT specialist
DrNarendra
1000
Nephrologist
DrWhite
10000
Pulmonologist
DrSummers
1500
Opthalmologist
DrStark
12000
Cardiologist
DrBanner
20000
Radiologist

Everything is printed in the next line. I have a project where I have to store details in an enumerator and print them out. And it would not be useful if I just print them out in a long meaningless list. I want to have the attributes side by side instead of everything being printed in the next line.


Solution

  • Use System.out.print() fort he first 2 print statements. Keep the last one as System.out.println();

    Also add spaces to the end of the first 2 println statements.

    public static void main(String args[])
    {
        for(DocDetails d:DocDetails.values())
        {
            System.out.print(d + " ");
            System.out.print(d.x + " ");
            System.out.println(d.z);
        }
    }
    

    }