fork download
  1. import scala.util.{Try, Success, Failure}
  2.  
  3.  
  4.  
  5. object Main extends App {
  6. def divide: Try[Int] = {
  7. val dividend = Try(Console.readLine("Enter an Int that you'd like to divide:\n").toInt)
  8. val divisor = Try(Console.readLine("Enter an Int that you'd like to divide by:\n").toInt)
  9. val problem = dividend.flatMap(x => divisor.map(y => x/y))
  10. problem match {
  11. case Success(v) =>
  12. println("Result of " + dividend.get + "/"+ divisor.get +" is: " + v)
  13. Success(v)
  14. case Failure(e) =>
  15. println("You must've divided by zero or entered something that's not an Int. Try again!")
  16. println("Info from the exception: " + e.getMessage)
  17. divide
  18. }
  19. }
  20.  
  21. def main() {
  22. divide
  23. }
  24. }
Success #stdin #stdout 0.23s 382656KB
stdin
42
0
stdout
Standard output is empty