This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

https://codingbat.com/prob/p190570 - boolean and String

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

On codingbat exercise https://codingbat.com/prob/p190570

Questions:
1) I'm getting the error message incompatible types: boolean cannot be converted to java.lang.String.  I've researched a bit, but I'm not entirely sure why I'm apparently trying to convert a boolean to a String.
2) Also I interpreted they were asking me to code for the following checks:  
- the string not to be empty
- the n to be within a certain range
but their solution has no conditions.

The problem reads:
"Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive).

missingChar("kitten", 1) → "ktten"
missingChar("kitten", 0) → "itten"
missingChar("kitten", 4) → "kittn"

My solution:



Their solution:


 
Saloon Keeper
Posts: 3411
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Pedro,

your method definition says that a String will be returned (line 1). However, in line 9 you return a boolean. That is not a String, so the compiler gives you that error

But you do not need that checking at all. If you look at the constraints of the exercise, they guarantee that the input will be valid. So, in particular, the String cannot be empty, since then no index would be valid, and the index is always minimal 0 and maximal s.length() - 1.
 
Pedro Esgueira
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet, thanks

Of course! The method type needs to match the return type.. *facepalm. I was thinking that the error was in my boolean logic... but its the return type. Got it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!