1

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

Community
  • 1
  • 1
Pola
  • 11
  • 3

4 Answers4

0

The compiler evaluates the constant expression 'B' + 'A' + 'T' by promoting each of the char-valued operands ('B', 'A' and 'T') to int values through a process known as widening primitive conversion.

Take a look to the ASCII table (http://www.asciitable.com/) to get the value of each char:

  • B = 66
  • A = 65
  • T = 84

So 'B' + 'A' + 'T' is equivalent to the int constant 66 + 65 + 84 = 215.

If you want BAT to be printed, you need to create a new string. Something like this should work:

System.out.print("" + names[0].charAt(0) + names[1].charAt(0) + names[2].charAt(0) + "\n");
Pier-Alexandre Bouchard
  • 5,135
  • 5
  • 37
  • 72
0

215 is the ASCII summation of B+A+T (66+65+84), basically instead of concatenating characters it thinks you are adding them. You can get around this by using printf() instead of a normal print:

System.out.printf("%s%s%s",names[0].charAt(0),names[1].charAt(0),names[2].charAt(0));
Gorfield
  • 36
  • 3
0

The following code is a possible solution:

System.out.print(names[0].substring(0));
System.out.print(names[1].substring(0));
System.out.print(names[2].substring(0));
System.out.println("");

Where you just print out each letter separate and then use print("\n") to do the new line

jgr208
  • 2,896
  • 9
  • 36
  • 64
0

You have to concatenate those letters like this:

System.out.print(names[0].charAt(0) +""+ names[1].charAt(0) +""+ names[2].charAt(0) + "\n");

Because in your case you are summing the ASCII codes of those Chars instead of concatenating them, that's why you got 215.

Or assign their values to your initialsstring, then print it:

initials+=names[0].charAt(0);
initials+=names[1].charAt(0); 
initials+=names[2].charAt(0);
System.out.print(intitials);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78