Eric Lang
Eric Lang

Reputation: 53

For loop for printing letters

public class LetterPrint {
    int totalLines;
    int consecLines;
    int timesPrintedinLine;
    int linesPrinted;
    char currentChar;
    LetterPrint(int totalLines, int consecLines){
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }
    void beginPrinting(){
        for(timesPrintedinLine = 0; linesPrinted<=totalLines; timesPrintedinLine++)
        {
            Print();
        }
    }
    public char correctChar(){
        if(timesPrintedinLine/(consecLines*4) == 1)
            return currentChar = 'A';
        else 
            return currentChar = 'B';

    }
    void Print(){
        if (timesPrintedinLine%5 !=0)
            System.out.print(correctChar());
        else{
            System.out.println();
            linesPrinted = timesPrintedinLine/4;
        }

    }
}

Can anyone help me see why, when created with "LetterPrint letterPrinter = new LetterPrint(6,1);" this object is printing

BBBA
AABB
BBBB
BBBB
BBBB
BBBB 

rather than

AAAA
BBBB
AAAA
BBBB
AAAA
BBBB

I appreciate anyone who can clear this up for me.

Upvotes: 4

Views: 242

Answers (4)

Carlos Herrera Plata
Carlos Herrera Plata

Reputation: 993

first, timesPrintedinLine and linesPrinted for conventions and best practices need to be declared as local variables. Second try to not call to often the System.out

Now the correct code can be this:

public class LetterPrint {
   private static final int CHARS_BY_CONSLINES = 4; 

   private int totalLines;
   private int consecLines;

   LetterPrint(int totalLines, int consecLines){
       this.totalLines = totalLines;
       this.consecLines = consecLines;
   }

   void beginPrinting(){
       String text;
       for(int rows = 0; rows < totalLines; rows++){
           text = "";
           for (int cols = 0; cols < consecLines*CHARS_BY_CONSLINES; cols++) {
               text += printChar(rows);
           }
           System.out.println(text);
       }
   } 

   void printChar(int row){
       if (row%2==0)
           return "A";
       else
           return "B"
   }

}

Upvotes: 1

Bhargav Rao
Bhargav Rao

Reputation: 52071

Corrected your code to the maximum possible extent.

public class LetterPrint {
    int totalLines;
    int consecLines;
    int timesPrintedinLine;
    int linesPrinted;
    char currentChar;
    LetterPrint(int totalLines, int consecLines){
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }
    void beginPrinting(){
        for(timesPrintedinLine = 1; linesPrinted<=totalLines; timesPrintedinLine++)
        {
            Print();
        }
    }
    public char correctChar(){
        if((timesPrintedinLine/(consecLines*4+1)) %2  == 0)
            return currentChar = 'A';
        else 
            return currentChar = 'B';

    }
    void Print(){
        if (timesPrintedinLine%5 !=0)
            System.out.print(correctChar());
        else{
            System.out.println();
            linesPrinted = timesPrintedinLine/4;
        }

    }

    public static void main(String ar[])
    {
     LetterPrint LETTERPRINTER = new LetterPrint(6,1);
     LETTERPRINTER.beginPrinting();
    }
}

The output is as expected.

AAAA
BBBB
AAAA
BBBB
AAAA
BBBB

The change made are

  1. The for loop starts from 1 to avoid the printing of the first blank line.
  2. The condition in the function correctChar has been changed to accommodate subsequent changes. (Yours was restricted to the first iteration)

If you want explanation, please ask.

Upvotes: 1

TNT
TNT

Reputation: 2920

Step through each process. timesPrintedInLine is first 0, so in Print() it will print a new line. When timesPrintedInLine is 1, 1 is not a multiple of 5, so the System.out.print will print the char returned from correctChar() in Print(); because 1/(1*4) does not equal 1, B is printed. Same happens for 2 and 3.

When timesPrintedInLine is 4, we go to correctChar() again; A is printed this time.

timesPrintedInLine now becomes 5, so a new line is printed, and linesPrinted is now equal to 5/4, or 1.

For timesPrintedInLine = 6 and 7, A is printed since 6/4 and 7/4 equal 1.

From this point on, timesPrintedInLine will always be greater than 7. Subsequently, timesPrintedInLine/(1*4) can never equal 1, so that is why B is always printed (except for when timesPrintedInLine is a multiple of 5; in this case a new line is printed and linesPrinted equals timesPrintedInLine/4).

The problem is coming from your if statement, so change it to this:

if(timesPrintedinLine/(consecLines*5) % 2 == 0)

Upvotes: 1

Regent
Regent

Reputation: 5178

Well, since the original code isn't "the best one", I have rewritten it:

public class LetterPrint
{
    private final static int charsInLine = 4;

    private final int totalLines;
    private final int consecLines;

    public LetterPrint(int totalLines, int consecLines)
    {
        this.totalLines = totalLines;
        this.consecLines = consecLines;
    }

    public void beginPrinting()
    {
        for (int lineNumber = 0; lineNumber < totalLines; lineNumber++)
        {
            char currentChar = getChar(lineNumber);
            for (int charNumber = 0; charNumber < charsInLine; charNumber++)
            {
                System.out.print(currentChar);
            }
            System.out.println();
        }
    }

    private char getChar(int lineNumber)
    {
        if ((lineNumber / consecLines) % 2 == 0)
        {
            return 'A';
        }
        return 'B';
    }
}

Upvotes: 1

Related Questions