I'm very, VERY new to Java so apologies if this is something very obvious that I just fail to see. I'm doing a course on Java at my uni and we've just started to cover arrays. There was a simple exercise we were assigned:
Write a program that creates an array (names) with the names Bob, Ann & Tom. The program should go through the array and print out the first letter of each name as a word ("BAT").
We are given a way to do it using a for loop and this method works:
String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
for (int i = 0; i < names.length; i++)
{
initials = initials + names[i].charAt(0);
}
System.out.print(initials);
But I wanted to do it in another way, using char.At() method:
System.out.print(names[0].charAt(0) + names[1].charAt(0) + names[2].charAt(0) + "\n");
However, instead of "BAT", the output is 215. Can anyone explain why this is happening and what's wrong with my code? Many, many thanks!
==== EDIT:
I have now tried the four solutions that chsdk, jgr208, Pier-Alexandre Bouchard and Gorfield suggested, however none of them works. I still end up with 215. I've also tried methods of converting chars to strings mentioned in this thread; I still end up with 215. This got me thinking maybe there is something wrong with my declaration of the array? Any suggestions? Code details of what I've tried below.
String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
initials = initials + names[0].charAt(0);
initials = initials + names[1].charAt(0);
initials = initials + names[2].charAt(0);
System.out.print(initials);
= 215
String [] names = {"Bob", "Ann", "Tom"};
String a = names[0].charAt(0) + "";
String b = names[1].charAt(0) + "";
String c = names[2].charAt(0) + "";
System.out.print("" + a + "" + b + "" + c + "");
= 215
String [] names = {"Bob", "Ann", "Tom"};
String a = new Character(names[0].charAt(0)).toString();
String b = new Character(names[1].charAt(0)).toString();
String c = new Character(names[2].charAt(0)).toString();
System.out.print("" + a + "" + b + "" + c + "");
= 215
String [] names = {"Bob", "Ann", "Tom"};
String a = Character.toString(names[0].charAt(0));
String b = Character.toString(names[1].charAt(0));
String c = Character.toString(names[2].charAt(0));
System.out.print("" + a + "" + b + "" + c + "");
= 215
String [] names = {"Bob", "Ann", "Tom"};
String a = String.valueOf(names[0].charAt(0));
String b = String.valueOf(names[1].charAt(0));
String c = String.valueOf(names[2].charAt(0));
System.out.println(a + "" + b + "" + c);
= 215
String [] names = {"Bob", "Ann", "Tom"};
System.out.printf("%s%s%s",names[0].charAt(0),names[1].charAt(0),names[2].charAt(0));
= 215
String [] names = {"Bob", "Ann", "Tom"};
System.out.print(names[0].substring(0));
System.out.print("\n");
System.out.print(names[1].substring(0));
System.out.print("\n");
System.out.print(names[2].substring(0));
System.out.println("");
= 215
String [] names = {"Bob", "Ann", "Tom"};
String initials = "";
initials+=names[0].charAt(0);
initials+=names[1].charAt(0);
initials+=names[2].charAt(0);
System.out.print(initials);
= 215
String [] names = {"Bob", "Ann", "Tom"};
System.out.print("" + names[0].charAt(0) + "" + names[1].charAt(0) + "" + names[2].charAt(0) + "\n");`
= 215