• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String challenge.

 
Ranch Hand
Posts: 62
2
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic