-1

I have an array printing very strangely. Code and output is below.

public class AdvDotComLauncher {
    public static void main(String[] args) {
        AdvDotComTable table = new AdvDotComTable();
        table.createTable(5,5, 5);
    }
}

Code with problematic array below.

import java.util.ArrayList;


public class AdvDotComTable {
    public void createTable(int size, int dotComAmount, int dotComSize) {
        //Holds the DotComs that will be randomly put onto the map
        String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"};
        //Holds the rows and columns
        ArrayList<Character> row = new ArrayList<Character>();
        ArrayList<Integer> column = new ArrayList<Integer>();
        column.add(0);
        char[] theAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        //Makes sure the Dot Coms will fit
        if (dotComAmount >= dotComs.length || dotComAmount / dotComSize > size) {
            System.out.println("There are too many dot coms! Please enter different amounts.");
        }
        //Used in the while loop to make sure that the correct amount of columns are created
        int done = size * size;
        //Used to get an index value in theAlphabet
        int theAlphabetIndex = 0;
        //Used to add columns
        int number = 0;
        //Used to tell when to create a column
        int size2 = size;
        //Starts creating rows and columns
        while (done > 0) {
            row.add(theAlphabet[theAlphabetIndex]);
            if (size2 == 0) {
                size2 = size;
                column.add(number);
                number++;
                theAlphabetIndex++;
            }
            System.out.println(row.get(number) + "" + column.get(number));
            size2--;
            done--;
        }
        System.out.println("The table has been created with " + dotComAmount  + " Dot Coms, a table size of " + size + ", and a Dot Com size of " + dotComSize + ".");
    }
}

Output:

A0
A0
A0
A0
A0
A0
A0
A0
A0
A0
A1
A1
A1
A1
A1
A2
A2
A2
A2
A2
A3
A3
A3
A3
A3
The table has been created with 5 Dot Coms, a table size of 5, and a Dot Com size of 5.

The expected output is this:

A0
B0
C0
D0
E0
A1
B1
C1
D1
E1
A2
B2
C2
D2
E2
A3
B3
C3
D3
E3
A1
B4
C4
D4
E4

I have no idea what is causing this problem, and any assistance would be appreciated.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Lyfe
  • 79
  • 1
  • 8
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) --- By far the best assistance we can give you, is to suggest you learn how to debug your code, so you can find problems yourself, rather than have to wait on others to debug it for you. You learn more that way too. – Andreas Oct 08 '17 at 18:13
  • I can fix the numbers by swapping around the lines `column.add(number);` and `number++;`. I'm completely confused about what you're trying to do with the letters though. – Paul Boddington Oct 08 '17 at 18:14

1 Answers1

0

Look at your program again theAlphabetIndex is never increase, you set it to 0 but it not increase as value of size2 is 5. As theAlphabetIndex=0 you are getting only A(..) which is at 0 index.

Manish Jaiswal
  • 442
  • 5
  • 19
  • You are wrong. I put a System.out.println("test"); statement inside the if statement, and it was printed. Also, size2 is decreased every time the loop goes through... – Lyfe Oct 08 '17 at 20:55
  • Then why you are getting a0(10 times) ,a1(5times) and so on . Only your while loop run properly 25 times. – Manish Jaiswal Oct 09 '17 at 02:06