• Post Reply Bookmark Topic Watch Topic
  • New Topic

String challenge.  RSS feed

 
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm trying out a challenge on CodingBat.  This is the challenge:

Given 2 strings, a and b, return a new string made of the first char of a and the last char of b, so "yo" and "java" yields "ya". If either string is length 0, use '@' for its missing char.

lastChars("last", "chars") → "ls"
lastChars("yo", "java") → "ya"
lastChars("hi", "") → "h@"
/quote]

This is the code i have so far:



and this is the error it is giving me:


Error: result = (a.charAt(0)) + (b.charAt(b.length()-1));
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Type mismatch: cannot convert from int to String



I thought the result would be a string and not an integer?  Is there something obvious I'm missing here?  Thanks.
 
Greenhorn
Posts: 3
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually String class's charAt() method returns char that is nothing but int ASCII value. So In your code you are actually not concatinating but adding two integers and trying to store in result that is of type String. You could do something like this :

 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More specifically, char is a numeric integral primitive type, and performing a mathematical operation on numeric types results in an int or long value.

JLS 4.2 -- specifically 4.2.1 & 4.2.2
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abinash Singh wrote:Actually String class's charAt() method returns char that is nothing but int ASCII value. So In your code you are actually not concatinating but adding two integers and trying to store in result that is of type String. You could do something like this :



After reading the link Darryl posted, i noticed this part:
The string concatenation operator + (§15.18.1), which, when given a String operand and an integral operand, will convert the integral operand to a String representing its value in decimal form, and then produce a newly created String that is the concatenation of the two strings
 

So your code makes sense to me now.  Thanks guys!
 
Abinash Singh
Greenhorn
Posts: 3
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abinash Singh wrote:Actually String class's charAt() method returns char that is nothing but int ASCII value. So In your code you are actually not concatinating but adding two integers and trying to store in result that is of type String. You could do something like this :

 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Abinash, welcome to the Ranch!

Thank you for your contribution.  Just a note that in the Beginning Java forum, we discourage posting complete solutions.  We want the OP to discover their own solutions, so please encourage and hint and post a snippet of code, but don't solve the problem for them.  Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!