Why the method of the following class is getting invoked several times even if the condition is false.Please help me with my code...

public class PRDDayBook {

    int remX;

    public PRDDayBook(int x) {
        remX = x;

        PrinterJob pj = PrinterJob.getPrinterJob();

        PageFormat pf = pj.defaultPage();
        Paper paper = new Paper();
        double margin = 9;
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);

        pj.setPrintable(new MyPrintable(), pf);
        if (pj.printDialog()) {
            try {
            } catch (PrinterException e) {

    class MyPrintable implements Printable {

        public int print(Graphics g, PageFormat pf, int pageIndex) {
            if (pageIndex != 0) {
                return NO_SUCH_PAGE;


            int x = 100;
            int y = 50;

            Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Serif", Font.PLAIN, 10));

            g2.drawString("S.No", x, 30);
            g2.drawString("Date", x + 30, 30);
            g2.drawString("Particulars", x + 100, 30);
            g2.drawString("Amount Rs", x + 250, 30);
            g2.drawString("Voucher No", x + 300, 30);

            // for printing the items with teir rates and cost
            int hor = DayBookRecords.data.size();
            int ver = DayBookRecords.data.get(0).size();

            double ph = pf.getImageableHeight();
            double pw = pf.getImageableWidth();
            int m=0;

            System.out.println("HOR:" + hor + " VER " + ver);

            for (int i = remX + 1; i < hor; i++) {
                for (int j = 0; j < ver; j++) {

                    g2.drawString("" + DayBookRecords.data.get(i).get(j), x, y);

                    if (j == 0) {
                        x = x + 30;
                    if (j == 1) {
                        x = x + 70;
                    if (j == 2) {
                        x = x + 150;
                    if (j == 3 || j == 4) {
                        x = x + 50;
                x = 100;
                y = y + 15;
                if (y > ph) {
                    m = i;
                    new PRDDayBook(m);

            Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight());
            return PAGE_EXISTS;

Here i'm trying to print data to multiple pages in another way... everything works fine but the last page is printed more than one times. I cannot figure out where am i making mistake .Please Help!!

Could you include the import statements so the code will compile?

Also I don't see a main() method for testing the code.

actually its a part of printing module... there is another main method in MainFrame class i don't know how to make it compilable for u

that is a class which is called from another main class...i dun know how to show main class

The recursive call on line 84 is ringing an alarm bell, but the variables names are so totally useless (what's m supposed to be?) that I can't tell any more than that.

what method is invoked several times? how can we answer this if we don't get to see the code where the method (might be/)is called?

I supposed to make that recursive call just to print the remaining data(line no. 84). Everthing is working fine.. i can get as many pages of data as i want to print out.. but the problem is that for multiple pages of data i'm getting their PRINT_OUT more than one time.
I tried to figure out the no. of copies of data in this pattern:

For 1 page data :page no 1 -> 1 time
For 2 page data :page no 1 -> 1 time page no 2 -> 2 times
For 3 page data :page no 1 -> 1 time page no 2 -> 2 times page no 3 -> 4 times

this is how i am getting the printouts..how can i solve this problem plz help...

Which loop is the break on line 85 intended to exit? Without comments or useful variable names your code is more or less incomprehensible, but at at guess its trying to print one page then recurse to print the next?
Because you are only breaking out of the inner loop maybe the outer loop is continuing to print pages while the recursive call is also printing them, thus giving you more and more copies of the later pages?

on line 85 that break is to avoid the further prompt for the new PRDDayBook(i)...which wud have been called for everypossible value of i from the 1st loop.... I'm still stucked with the same problem i am still getting multiple copies of final pages...wud u please suggest me the alternative ways. Plzzz help

Can you post code for testing? The posted code does not have a main() method.
Something small that will compile, execute and show the problem.

thanx for all i solve the problem :) cheers