• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 25
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5656
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic