Nicholas Zachariah
Nicholas Zachariah

Reputation: 55

How to use a nested While loops to display Rows and Columns in Java

I am trying to come up with the following output:

Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B

I am having trouble figuring out the logic to complete this task. So far I came up with the code provided below:

int iRowNum;
int iColLetter;

iRowNum = 1;
iColLetter = 65;

while (iColLetter < 67)
{

  System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);

  iColLetter++;

  while (iRowNum < 3)
  {
    iRowNum += 1;
  }
}

Unfortunately, I receive the output of:

Row 1 Col A
Row 3 Col B

That begin said, I have a feeling I am getting close to where I need to be, but I spent a good chunk of time trying to figure out the logic behind my desired output.

Final Question How do I display the rows and columns as shown in the first block of this post?

Upvotes: 1

Views: 1766

Answers (6)

Ta_Req
Ta_Req

Reputation: 106

Your output has only two rows that mean the program is not getting into the inner while loop to print. The reason is iColLetter = 65 and it increased to 57 and stop going inside while loop. So you have to reset iColLetter = 65 in the outer loop. Look at the example

public class NestWhileLoop {
    public static void main(String [] args) {
        int iRowNum = 1;
        int iColLetter = 65;


        while (iRowNum < 3) {

            while (iColLetter < 67){

                System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);

                iColLetter++;
            }
            iColLetter = 65;
            iRowNum ++;

        }

   }

}

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

Do it as follows:

public class Main {
    public static void main(String[] args) {
        int iRowNum = 1;
        int iColLetter = 65;
        while (iRowNum < 3) {
            iColLetter = 65;
            while (iColLetter < 67) {
                System.out.println("Row " + iRowNum + " Col " + (char) iColLetter);
                iColLetter++;
            }
            iRowNum += 1;
        }
    }
}

Output:

Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B

Explanation:

  1. Since you want the iRowNum to go up to 2, you start with while (iRowNum < 3) or while (iRowNum <= 2).
  2. For each value of iRowNum, the inner loop has to run for two times, starting with 65 i.e. you need to reset iColLetter to 65 before the start of the inner loop.

Upvotes: 1

Scorpion
Scorpion

Reputation: 3986

I have pasted an answer as close as possible to your code. I had to make several minor changes - you should analyze each one to understand the subtle differences each line of code on how loops work, and how in some cases you need to reinitialize variables/ restructure for loops.

int iRowNum = 1;
int iColLetter = 65;

while (iRowNum < 3)
{    
  while (iColLetter < 67)
  {      
    System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);//print alphabet for the given row
    iColLetter++;//increment the alphabet
  }
  iRowNum += 1;//row 1 printed, point to row 2
  iColLetter=65;//row 1 printed, reinitialize to start printing at A
}

  }

Upvotes: 0

Adnan
Adnan

Reputation: 496

    iRowNum = 1;
    iColLetter = 65;

    while (iRowNum < 3) {
        iColLetter = 65;
        while (iColLetter < 67) {

            System.out.println("Row " + iRowNum + " Col " + (char) iColLetter);

            iColLetter++;


        }
        iRowNum += 1;
    }

Output

Row 1 Col A
Row 1 Col B
Row 2 Col A
Row 2 Col B

Upvotes: 0

VHS
VHS

Reputation: 10184

You are looping through the rows first and then columns. That's why you are not getting the desired output. See the following correction in your code:

iRowNum = 1;
while (iRowNum < 3)
{
   iColLetter = 65;
   while (iColLetter < 67)
   {  
      System.out.println("Row " + iRowNum + " Col " + (char)iColLetter);    
      iColLetter++;
   }    
   iRowNum += 1;
}

Upvotes: 0

Andronicus
Andronicus

Reputation: 26076

You have mixed up the ordering of incrementing. This should do the job:

iRowNum = 1;
iColLetter = 65;

while (iRowNum < 3) {
  for(int i = 0; i + iColLetter < 67; i++){
    System.out.println("Row " + iRowNum + " Col " + (char)(iColLetter + i));
  }
  iRowNum += 1;
}

Upvotes: 1

Related Questions