Please help to solve this

Write an implicit function which takes a string as a parameter and converts it to a CustomString. Write another implicit function which takes a string and converts it to CustomInt.

i done the following code:

import scala.language.implicitConversions
object ImplicitConversion extends App {
class CustomString(val s: String) {
def findLength = s.length
}
class CustomInt(val number : Int){
def multiply = number*5
}

implicit def StrToCustomString(s:String):CustomString = new CustomString(s)

implicit def StrToCustomInt(output2:String):CustomString = new CustomString(s)
val output1 = “100”.findLength
val output2 = “100”.multiply
println(output1, output2)
}

Does the code you have give a compile error, runtime error, other unexpected behavior, or what?

What kind of help are you looking for, exactly? Can you explain where you got stuck?

1 Like

I am getting compiler error in implenting second method-> “StrToCustomInt”.I am confusing about this.please tell.

What does the compiler error say?

Both functions are returning CustomString. None are returning CustomInt.

1 Like

yes posco, I am not getting customInt …function.could you please tell what can be the modification in that ?

@SethTisue
on modifying the line :implicit def StrToCustomInt(s:String):CustomString = new CustomString(number)

i am getting 4 errors, i am attaching the image

guys the output would be: (3,500)

@BalmungSan
can you please assist me on this problem ?

Oscar told you what the problem with your code is. Were you able to use his advice? If so, what does your code look like now?

The error message in the screen shot provided appears to show that you haven’t fixed the problem Oscar pointed out.

import scala.language.implicitConversions
object ImplicitConversion extends App {
class CustomString(val s: String) {
def findLength = s.length
}
class CustomInt(val number : Int){
def multiply = number*5
}

implicit def StrToCustomString(s:String):CustomString = new CustomString(s)

implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(s)
val output1 = “100”.findLength
val output2 = “100”.multiply
println(output1, output2)
}

i am getting the following error:
$ scalac ImplicitConversion.scala
ImplicitConversion.scala:13: error: illegal character ‘\u201c’
val output1 = “100”.findLength
^
ImplicitConversion.scala:13: error: illegal character ‘\u201d’
val output1 = “100”.findLength
^
ImplicitConversion.scala:14: error: illegal character ‘\u201c’
val output2 = “100”.multiply
^
ImplicitConversion.scala:14: error: illegal character ‘\u201d’
val output2 = “100”.multiply
^
four errors found

You used the wrong quote characters: “ ” instead of " ". This usually happens when copy-pasting something from a pdf.

2 Likes

I have corrected the double quote , but now i am getting following error:
$ scalac ImplicitConversion.scala
ImplicitConversion.scala:12: error: not found: value s
implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(s)
^
one error found

That error message is obvious - you called your argument output2 but tried to use s in the function definition. But you will get another error when you correct this, which you solve using .toInt for strings (if you can be unsafe, otherwise you should test if s is an integer).

regards,
Siddhartha

1 Like

Again Thank you sir,
I cleared the code by implementing:

implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(output2.toInt)

Thank you so much

class CustomString(val s: String) {
def findLength = s.length
}
class CustomInt(val number : Int){
def multiply = number*5
}

implicit def StrToCustomString(s:String):CustomString = new CustomString(s)

implicit def StrToCustomInt(output2:String):CustomInt = new CustomInt(output2.toInt)

val output1 = “100”.findLength
val output2 = “100”.multiply
println(output1, output2)

This is identical code to what @rjrishabh507 posted on June 16, isn’t it?

What kind of help with it do you want? What is your question? Have you reviewed the previous posts in this thread?

Make sure your double-quote characters are correct.