1

I'm trying to make a loop here, but that will print multiple number combinations of 1-2 for digits ## + ##. But for now I can't get my numD in the last loop to print out. For some reason, the numD won't even concatenate, or add incrementally. Any help appreciated, you can ignore the commented code

dim = 2;
numA = 1;
numB = 1;
numC = 1;
numD = 1;
for (int numA = 1; numA < dim + 1; numA++) { // biggest or leftest number loop first

                for (int numB = 1; numB < dim + 1; numB++) { // second number from the left

                    for (int numC = 1; numC < dim + 1; numC++) { // third number from the left

                        for (int numD = 1; numD < dim + 1; numD++) { // least number loop last            
                        // this will store number in arraylist
                            //outcomeOutput = String.valueOf(numA) + String.valueOf(numB)+" + "
                                //  + String.valueOf(numC) + String.valueOf(numD); 

                            System.out.println(numD);

                            System.out.println(outcomeOutput);
                            //dBArray.get(dim-1).add(outcomeOutput);
                            //System.out.println(dBArray.get(dim-1).get(count));
                            //count++;
                        }
                    }
                }


        }
whydoubt
  • 674
  • 1
  • 6
  • 14
Dev
  • 13
  • 3
  • Welcome to Stack Overflow. I'd suggest you remove any comments that aren't relevant to the question as they make the code more confusing for answerers. It would also be good if you put in some effort to simplify the problem, e.g. make an example that demonstrates the issue, but uses only one or two loops rather than four loops. https://stackoverflow.com/help/how-to-ask – ᴇʟᴇvᴀтᴇ Oct 02 '19 at 19:14

2 Answers2

1

You don't need to (and shouldn't) initialize the loop variables before the loops.

If you uncomment the lines:

String outcomeOutput = String.valueOf(numA) + String.valueOf(numB)+" + "
                          + String.valueOf(numC) + String.valueOf(numD);

System.out.println(outcomeOutput);

It seems to print what you want.

There are actually easier ways to turn ints into strings. You can use String.format() (e.g. String.format("%d%d + %d%d", numA, numB, numC, numD) Or, even easier, you can concatenate them to a string. In the code below I start with an empty string "" and then concatenate the ints to it. I've used simpler variable names, which I think make the code easier to read.

int dim = 2;
for (int a = 1; a < dim + 1; a++) {
    for (int b = 1; b < dim + 1; b++) {
        for (int c = 1; c < dim + 1; c++) {
            for (int d = 1; d < dim + 1; d++) {
                System.out.println("" + a + b + " + " + c + d);
            }
        }
    }
}

Produces:

11 + 11
11 + 12
11 + 21
11 + 22
12 + 11
12 + 12
12 + 21
12 + 22
21 + 11
21 + 12
21 + 21
21 + 22
22 + 11
22 + 12
22 + 21
22 + 22

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Thank you! This seems to help a lot. Exactly what I'm trying to achieve. I'm also wondering if my version of Java is outdated. Before I tried something very similar to this code (not the same), but I didn't get anything printed. Do you also have an idea of how to store these values in a 2D arraylist? For example if dim == 1, then for the first row of the 2d arraylist, the first column would be "11 + 11", and for the next row of the arraylist, the second column would be these computed values in String form. – Dev Oct 04 '19 at 00:01
0

I might see it..... I just saw Dim was declared. I didn't think it was declared, I didn't see it until after I posted.

Your variable of numD is an integer. You must make it a string to be displayed by Println. take a look at this posting concerning Println How does System.out.print() work?