1

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?

1
  • Place iColLetter++ after the inner while loop ( while (iRowNum < 3)) Commented Mar 11, 2020 at 19:06

6 Answers 6

1

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

Comments

0

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;
}

Comments

0
    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

Comments

0

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
}

  }

Comments

0

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 ++;

        }

   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.