[Solved] Trying to display contents of ArrayList. Getting [Ljava.lang.String;@232204a1

I am attempting to output the contents of an ArrayList, but no matter which approach I try I seem get the location of the Array rather than the contents of the Array. Running each of the following together gives me:

 run:
 [[Ljava.lang.String;@55f96302, [Ljava.lang.String;@232204a1, [Ljava.lang.String;@4554617c, [Ljava.lang.String;@7f31245a, [Ljava.lang.String;@2503dbd3, [Ljava.lang.String;@5cad8086]


 [Ljava.lang.String;@232204a1
 [Ljava.lang.String;@232204a1
 [Ljava.lang.String;@232204a1

Here’s the code snippet:

// Each of the following approaches results in 
// [Ljava.lang.String;@232204a1 
// instead of the actual value of the ArrayList.

String test = accountNumbers.get(1);
System.out.println(test);

System.out.println(accountNumbers.get(1));

System.out.println(accountNumbers.get(1).toString());

// This actually outputs:
// [[Ljava.lang.String;@55f96302, [Ljava.lang.String;@232204a1, [Ljava.lang.String;@4554617c, [Ljava.lang.String;@7f31245a, [Ljava.lang.String;@2503dbd3, [Ljava.lang.String;@5cad8086] 
String str = Arrays.toString(accountNumbers.toArray()); 
System.out.println(str);

I’m not really sure what’s causing this. Is there some way to get the contents to display?

EDIT: Here’s the entire method. An answer on another question (here) advised me to try using ArrayList instead of the approach I was using. I adapted the suggestion, but I felt that the problems were better placed in a new question rather than as an edit to that question.

protected static void loadAccountInformationFromFile() throws Exception 
{

    Scanner account = new Scanner(new File(INPUT_ACCOUNT_FILE)).useDelimiter(",");

        int sortCount = 1;

        List<String> accountNumbers = new ArrayList<>();
        List<String> firstNames = new ArrayList<>();
        List<String> lastNames = new ArrayList<>();
        List<String> balances = new ArrayList<>();
        List<String> lastVariables = new ArrayList<>();


    do {

        String[] temp1 = account.next().split(",");
        String temp2 = "" + temp1;

        if (sortCount == ACCOUNT_NUMBER_COUNT) {
            accountNumbers.add(temp2);
        } else if (sortCount == FIRST_NAME_COUNT) {
            firstNames.add(temp2);
        } else if (sortCount == LAST_NAME_COUNT) {
            lastNames.add(temp2);
        } else if (sortCount == BALANCE_COUNT) {
            balances.add(temp2);
        } else if (sortCount == LAST_VARIABLE_COUNT) {
            lastVariables.add(temp2);
        }

        if (sortCount < MAX_VALUES_PER_LINE) {
            sortCount++;
        } else {
            sortCount = 1;
        }
    } while (account.hasNext());


    // Each of the following approaches results in 
    // [Ljava.lang.String;@232204a1 
    // instead of the actual value of the ArrayList.

    String test = accountNumbers.get(1);
    System.out.println(test);

    System.out.println(accountNumbers.get(1));

    System.out.println(accountNumbers.get(1).toString());

    // This actually outputs:
    // [[Ljava.lang.String;@55f96302, [Ljava.lang.String;@232204a1, [Ljava.lang.String;@4554617c, [Ljava.lang.String;@7f31245a, [Ljava.lang.String;@2503dbd3, [Ljava.lang.String;@5cad8086] 
    String str = Arrays.toString(accountNumbers.toArray()); 
    System.out.println(str);

    account.close();

    // I want to adapt what I previously used to access the ArrayLists.

 //        Bank bank = new Bank();
 //            
 //        bank.openAccount(new CheckingAccount(10100, new      Customer("Adam", "Apple"),500.00,false));
 //        bank.openAccount(new CheckingAccount(10101, new Customer("Beatrice", "Bagel"),2000.00,true));
 //        bank.openAccount(new SavingsAccount(2010, new Customer("Adam", "Apple"),5000.00,0.02));
}

EDIT 2: Here are the class variables:

private final static String INPUT_ACCOUNT_FILE = "accountInfo.txt";
private static final int ACCOUNT_NUMBER_COUNT = 0;
private static final int FIRST_NAME_COUNT = 1;
private static final int LAST_NAME_COUNT = 2;
private static final int BALANCE_COUNT = 3;
private static final int LAST_VARIABLE_COUNT = 4; 
private final static int MAX_VALUES_PER_LINE = 5;

EDIT 3: For the benefit of those who may read this question late and be confused by some of the comments on the correct answer, part of my issue was related to an issue with the text file itself. This is an example of the formatting of the text file:

 10100,First,Last,Balance,value
 10101,First,Last,Balance,value
 20100,First,Last,Balance,value

Also: To get the ArrayLists to store the correct strings I had to change sortCount from:

int sortCount = 1;

to

int sortCount = 0;

Because when it was set at 1 it would store the first name in the account number string.

Solution #1:

The problem is not in your “displaying” but in the way you read the contents from the file.

Your code prints out correctly “addresses” because the strings in accountNumbers instance are really these values (because you put array of strings into one single string). So what really happens is that in your temp2 String is your temp1.toString().

You are using wrong delimiter (you should use default one for whitespaces instead):

Scanner account = new Scanner(new File(INPUT_ACCOUNT_FILE));

And then assign values like:

if (temp1.length > ACCOUNT_NUMBER_COUNT) {
   accountNumbers.add(temp1[ACCOUNT_NUMBER_COUNT]);
if (temp1.length > FIRST_NAME_COUNT) {
   firstNames.add(temp1[FIRST_NAME_COUNT]);
if (temp1.length > LAST_NAME_COUNT) {
   lastNames.add(temp1[LAST_NAME_COUNT]);
if (temp1.length > BALANCE_COUNT) {
   balances.add(temp1[BALANCE_COUNT]);
if (temp1.length > LAST_VARIABLE_COUNT) {
   lastVariables.add(temp1[LAST_VARIABLE_COUNT]);
}

Your temp2 and sort variables are not needed.

Anyway it is a bit weird to use these collections. I would rather suggest to do it like:

Scanner scanner = new Scanner(new File(INPUT_ACCOUNT_FILE));
Collection<Account> bank = new ArrayList<>();

while (scanner.hasNext()) {
   String[] fields = scanner.next().split(",");
   if (fields.length < MAX_VALUES_PER_LINE) {
      continue; // incomplete row, skip it or maybe throw some exception?
   }

   String number = fields[ACCOUNT_NUMBER_COUNT];
   Customer customer = new Customer(fields[FIRST_NAME_COUNT], fields[LAST_NAME_COUNT]);
   double balance = Double.valueOf(fields[BALANCE_COUNT]);
   String type = fields[LAST_VARIABLE_COUNT];

   Account a = null;
   switch (type) {
      case "N": {
         a = new CheckingAccount(number, customer, balance);
         break;
      }
      case "0.02": {
         a = new SavingsAccount(number, customer, balance);
         break;
      }
      default: {
         continue; // unknown type of account, skip it or maybe throw some exception?
      }
   }
   bank.add(a);
}
Respondent: zdenda.online

Solution #2:

String temp2 = "" + temp1;

You are trying to concat a blank with a string array, this is equals with

String temp2 = "" + temp1.toString();

Note that toString() of Array will return object references, not the value.
So you should try to convert array to some Java Collection class that implements the toString() method like ArrayList

String temp2 = "" + Arrays.asList(temp1).toString();

or you can also do

String temp2 = "" + Arrays.toString(temp1);

Both will give you the String value (and some “[” and “]” too, I guess, because of the toString() implementation of ArrayList and Arrays, you can work it out).

Respondent: Trong Hoang

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.