1


I have a print statement in a loop that should be iterating like this: "A0" "B1" "C2" "B3", "C4". It is instead returning 6567697173.
Here's the code:
MAIN METHOD

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

TABLE CLASS

import java.util.ArrayList;

public class AdvDotComTable {
    public boolean createTable(int rows, int columns) {
        if (rows == 26) {
            //When true is returned, the program will let the user know they have to choose a lower number
            return true;
        }
        String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"};
        ArrayList<Character> row = new ArrayList<Character>();
        ArrayList<Integer> column = new ArrayList<Integer>();
        char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        int number = 0;
        while (rows != 0 && columns != 0) {
            row.add(letters[number]);
            column.add(number);
            System.out.print(row.get(number) + column.get(number));
            number++;
            rows--;
            columns--;
        }
        return false;
    }
}

I have no idea what is causing this problem, and any assistance would be appreciated. I am an amateur developer just trying to learn, so it's probably a silly mistake (sorry about that).
Thanks in advance,
Lyfe

  • 1
    change your `print` to --> `System.out.print(""+row.get(number)+ column.get(number));` – Ousmane D. Oct 07 '17 at 23:49
  • 1
    That worked, and I have absolutely no idea why. Thank you so much! Would you mind explaining to me why that fixes it? Sorry, I'm pretty new to Java. –  Oct 07 '17 at 23:51
  • `Character + Integer` adds those 2 together as numbers, it doesn't concatenate them into a `String`. – Bernhard Barker Oct 07 '17 at 23:56
  • Almost duplicate: [How to concatenate characters in java?](https://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java) – Bernhard Barker Oct 07 '17 at 23:58

2 Answers2

1

As mentioned in the comments, it's printing the ASCII value of the character. Just replace your

  System.out.print(row.get(number) + column.get(number));

with

  System.out.print('"'+""+row.get(number) + column.get(number)+'"');

and the output will be as you desired

 "A0""B1""C2""D3""E4"
Kamal Chanda
  • 163
  • 2
  • 12
0

As said in the comments,

change

System.out.print(row.get(number) + column.get(number));

to:

System.out.print("" + row.get(number) + column.get(number));

You need to do this so you are printing a String and not a hashCode (which is an integer).

Coder
  • 1,175
  • 1
  • 12
  • 32