-1

Im just going to be upfront about this being for homework but I currently am not able to download java and check but will the code bellow cause any conversion errors?

char sentence[] = { 'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u'};
String output = "The sentence is: ";

for(int i = 0; 1 < sentence.length; i++)
  output+= sentence[i]; 

System.out.println(output);

3 Answers3

1

It would just be simpler to do this

char sentence[] = { 'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u'};
System.out.println("The sentence is: " + String.copyValueOf(sentence));
MoonMist
  • 1,232
  • 6
  • 22
1

The only problem your code has is in this line: for(int i = 0; 1 < sentence.length; i++) it should be: for(int i = 0; i < sentence.length; i++) other than that it will print your string follow by the "how are you"

Talkhak1313
  • 301
  • 2
  • 11
0

I have just opened jshell.

jshell> String c = "cat"
c ==> "cat"

jshell> c += 's'
$3 ==> "cats"

In Java, if you concatenate an object, that object's toString method is called. If you concatenate a primitive, it is automatically converted to a String.

jshell> ArrayList<Integer> al = new ArrayList<>();
al ==> []

jshell> al.add(4);
$5 ==> true

jshell> al.add(5)
$6 ==> true

jshell> c += al
$7 ==> "cats[4, 5]"

Your code should compile as is.

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45