I have seen other threads about this but none of them answered my question. The fact is I think that in my case the problem is not like the others.
Here is a link to the problem I tried to solve: http://projecteuler.net/problem=55
I am sure the algorithm is correct but what I suspect is that there is something going on with what Scala can convert to string. I don't know. Here is my code:
package projecteuler55
object PE55 extends App {
def rev ( x :BigInt) : BigInt = {
x.toString.reverse.toInt
}
def notPalindrome ( x:BigInt) : Boolean = {
if ( x != rev(x) ) true else false
}
def test (x:BigInt , steps:Int) : Boolean = {
if ( steps > 50 ) true
else if (notPalindrome(x) == false) false
else test ( x + rev(x) , steps +1)
}
var lychrel = 0
for (i<-10 until 10000){
if (test(i+rev(i),0)) lychrel += 1
}
println(lychrel)
}
and the error I get is this:
Exception in thread "main" java.lang.NumberFormatException: For input string: "2284457131"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:229)
at scala.collection.immutable.StringOps.toInt(StringOps.scala:31)
at projecteuler55.PE55$.rev(PE55.scala:8)
at projecteuler55.PE55$.notPalindrome(PE55.scala:14)
at projecteuler55.PE55$.test(PE55.scala:20)
at projecteuler55.PE55$$anonfun$1.apply$mcVI$sp(PE55.scala:26)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:141)
at projecteuler55.PE55$delayedInit$body.apply(PE55.scala:25)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at projecteuler55.PE55$.main(PE55.scala:3)
at projecteuler55.PE55.main(PE55.scala)
Why does this happen? I also use BigInt in case of an overflow but this does not seem to help.
Thanks in advance
toInt expects a string that can be parsed to fit into a regular Int, which 2284457131 can't. You want to use BigInt(x.toString.reverse).
Related
I am trying to write unit test cases for awazon s3 to upload files.I have used mokito.
Following is my code for inserting files in S3:
def insertingFilesInS3(path: String, file: File): Boolean = {
try {
s3client.putObject(BUCKET_NAME, path, file)
true
} catch {
case ex: Exception => info(s"File storage failed for $path $file" + ex.printStackTrace()); false
}}
So far I have written:
val s3: AmazonS3Client = mock[AmazonS3Client]("s3")
val messageDigest = MessageDigest.getInstance("MD5")
val bucket = "bucket"
val keyName = "keyName"
val file: File = mock[File]
val expectedResult: PutObjectResult = mock[PutObjectResult]
val objectmetadata: ObjectMetadata =mock[ObjectMetadata]
"return true when inserting files in s3" in {
when(s3.putObject(bucket, keyName, file).setMetadata(objectmetadata)).thenReturn(expectedResult)
val result = S3Util.insertingFilesInS3(keyName, file)
assert(!result)
}
Assert statemnt is throwing exception and I'm getting false as result.
I am getting null pointer exception :
java.lang.NullPointerException
at com.amazonaws.services.s3.internal.Mimetypes.getMimetype(Mimetypes.java:160)
at com.amazonaws.services.s3.internal.Mimetypes.getMimetype(Mimetypes.java:201)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1642)
at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1604)
at s3Utility.S3Util$class.insertingFilesInS3(S3Util.scala:15)
at s3Utility.S3Util$.insertingFilesInS3(S3Util.scala:52)
at com.codesquad.test.S3UtilityTest.S3UtilTest$$anonfun$1.apply(S3UtilTest.scala:28)
at com.codesquad.test.S3UtilityTest.S3UtilTest$$anonfun$1.apply(S3UtilTest.scala:20)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
at com.codesquad.test.S3UtilityTest.S3UtilTest.withFixture(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$class.invokeWithFixture$1(WordSpecLike.scala:1075)
at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.WordSpecLike$class.runTest(WordSpecLike.scala:1088)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$BeforeAndAfter$$super$runTest(S3UtilTest.scala:12)
at org.scalatest.BeforeAndAfter$class.runTest(BeforeAndAfter.scala:203)
at com.codesquad.test.S3UtilityTest.S3UtilTest.runTest(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
at org.scalatest.WordSpecLike$class.runTests(WordSpecLike.scala:1147)
at com.codesquad.test.S3UtilityTest.S3UtilTest.runTests(S3UtilTest.scala:12)
at org.scalatest.Suite$class.run(Suite.scala:1147)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$WordSpecLike$$super$run(S3UtilTest.scala:12)
at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
at org.scalatest.WordSpecLike$class.run(WordSpecLike.scala:1192)
at com.codesquad.test.S3UtilityTest.S3UtilTest.org$scalatest$BeforeAndAfter$$super$run(S3UtilTest.scala:12)
at org.scalatest.BeforeAndAfter$class.run(BeforeAndAfter.scala:258)
at com.codesquad.test.S3UtilityTest.S3UtilTest.run(S3UtilTest.scala:12)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340)
You are trying to call a method of a mocked object without stating what should it return. So first you need to state that. Also, you are chaining method calls to the s3 object. setMetadata() has no return value as its a Unit function, and you are asking it to return expectedResult object of type PutObjectResult which will give you a compilation error. Instead, try the following.
"return true when inserting files in s3" in {
when(s3.putObject(bucket, keyName, file)).thenReturn(expectedResult)
val result = S3Util.insertingFilesInS3(keyName, file)
assert(!result)
}
I have an RDD and I want create a new RDD with unique values, but I have an error.
The code:
val rdd = sc.textFile("/user/ergorenova/socialmedia/allus/archivosOrigen").map( _.split(",", -1) match {
case Array(caso, canal, lote, estado, estadoo, estadooo, fechacreacioncaso, fechacierrecaso, username, clientid, nombre, apellido, ani, email) =>(canal, username, ani, email)
}).distinct
val twtface = rdd.map {
case ( canal, username, ani, email ) =>
val campoAni = "ANI"
(campoAni , ani , canal , username)
}.distinct()
twtface.take(3).foreach(println)
This is the CSV file
caso2,canal2,lote,estado3,estado4,estado5,fechacreacioncaso2,fechacierrecaso2,username,clientid,nombre,apellido,ani,email
2694464,Twitter,Redes Sociales Movistar - Twitter,Cerrado por Abandono – Robot,,,16/04/2015 23:57:51,17/04/2015 6:00:19,kariniseta,158,,,22,mmmm#test.com
2694464,Twitter,Redes Sociales Movistar - Twitter,Cerrado por Abandono – Robot,,,16/04/2015 23:57:51,17/04/2015 6:00:19,kariniseta,158,,,22,mmmm#test.com
2635376,Facebook,Redes Sociales Movistar - Facebook,Cerrado por Abandono – Robot,,,03/04/2015 20:20:18,04/04/2015 2:30:06,martin.saggini,1126,,,,
2635376,Facebook,Redes Sociales Movistar - Facebook,Cerrado por Abandono – Robot,,,03/04/2015 20:20:18,04/04/2015 2:30:06,martin.saggini,1126,,,,
Error:
scala.MatchError: [Ljava.lang.String;#dea08cc (of class [Ljava.lang.String;)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$1.apply(<console>:21)
at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$1.apply(<console>:21)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at org.apache.spark.util.collection.ExternalSorter.insertAll(ExternalSorter.scala:211)
at org.apache.spark.shuffle.sort.SortShuffleWriter.write(SortShuffleWriter.scala:73)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:73)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
at org.apache.spark.scheduler.Task.run(Task.scala:88)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I assume the error is due to a missing/additional newline in your csv file.
Your split and match assumes that every line of the csv has exactly 14 fields. Depending on the encoding or text editor you use, you may have additional new lines at the end of the document.
My suggestion would be to validate each line and add a catch-all case that gives you a more detailed error message, that way you will avoid the ambiguous MatchError.
I have object DictionarySqlLiteDao which should create table of it not exist during its initialization. But I have exception despite the fact that the database is created in file system. Help me to figure out the reason of it. Here is my code:
object DictionarySqlLiteDao extends Dao[Word] {
private val CREATE_WORD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS thelper.WORD " +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"WORD TEXT NOT NULL, " +
"UKR TEXT NOT NULL, " +
"ENG TEXT NOT NULL, " +
"EXAMPLE TEXT)"
private val SAVE_WORD_SQL = "INSERT INTO WORD(WORD, UKR, ENG, EXAMPLE" +
"VALUES(?, ?, ?, ?)"
private val connection: Connection = {
Class.forName("org.sqlite.JDBC")
DriverManager.getConnection("jdbc:sqlite:thelper")
}
{
connection.createStatement().executeUpdate(CREATE_WORD_TABLE_SQL)
}
override def save(word: Word): Word = {
val statement = connection.prepareStatement(SAVE_WORD_SQL, Statement.RETURN_GENERATED_KEYS)
statement.setString(1, word.word)
statement.setString(2, word.translation("ukr"))
statement.setString(3, word.translation("eng"))
statement.setString(4, word.example.orNull)
statement.executeUpdate()
val id = statement.getGeneratedKeys.getInt(1)
word.copy(id = id)
}
def main(args: Array[String]) {
println(connection == null) // here I get false
println(connection.createStatement().execute("select date('now')")) // here I get true
val w = save(Word(-1, "germanWord", Map("ukr" -> "ukrTranslation", "eng" -> "engTranslation"), Option.empty[String])) // and here I get an Exception
println(w)
}
}
And here is an exception I get:
Exception in thread "main" java.lang.ExceptionInInitializerError
at thelper.Dictionary.dao.DictionarySqlLiteDao.main(DictionarySqlLiteDao.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLException: unknown database thelper
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<init>(DictionarySqlLiteDao.scala:23)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<clinit>(DictionarySqlLiteDao.scala)
... 6 more
As far as I remember you should specify full path to database, in your in-memory case it should be thelper.db like:
DriverManager.getConnection("jdbc:sqlite:thelper.db")`
P.S. After that you can omit database name: "CREATE TABLE IF NOT EXISTS WORD " + ....
P.P.S you can use triple quotes to write multiline strings like
"""CREATE TABLE IF NOT EXISTS WORD(
| ID INTEGER PRIMARY KEY AUTOINCREMENT,
| WORD TEXT NOT NULL,
| UKR TEXT NOT NULL,
| ENG TEXT NOT NULL,
| EXAMPLE TEXT)""".stripMargin
I am using play-framework 2.3.x with reactivemongo-extension JSON type. following is my code for fetch the data from db as below:
def getStoredAccessToken(authInfo: AuthInfo[User]) = {
println(">>>>>>>>>>>>>>>>>>>>>>: BEFORE"); //$doc("clientId" $eq authInfo.user.email, "userId" $eq authInfo.user._id.get)
var future = accessTokenService.findRandom(Json.obj("clientId" -> authInfo.user.email, "userId" -> authInfo.user._id.get));
println(">>>>>>>>>>>>>>>>>>>>>>: AFTER: "+future);
future.map { option => {
println("*************************** ")
println("***************************: "+option.isEmpty)
if (!option.isEmpty){
var accessToken = option.get;println(">>>>>>>>>>>>>>>>>>>>>>: BEFORE VALUE");
var value = Crypto.validateToken(accessToken.createdAt.value)
println(">>>>>>>>>>>>>>>>>>>>>>: "+value);
Some(scalaoauth2.provider.AccessToken(accessToken.accessToken, accessToken.refreshToken, authInfo.scope,
Some(value), new Date(accessToken.createdAt.value)))
}else{
Option.empty
}
}}
}
When i using BSONDao and BsonDocument for fetching the data, this code successfully run, but after converting to JSONDao i getting the following error:
Note: Some time this code will run but some it thrown an exception after converting to JSON
play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: bound must be positive
application -
Following are the logs of application full exception strack trace as below:
>>>>>>>>>>>>>>>>>>>>>>: BEFORE
>>>>>>>>>>>>>>>>>>>>>>: AFTER: scala.concurrent.impl.Promise$DefaultPromise#7f4703e3
play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: bound must be positive
application -
! #6m1520jff - Internal server error, for (POST) [/oauth2/token] ->
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: bound must be positive]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]
at scala.Option.map(Option.scala:146) [scala-library-2.11.6.jar:na]
Caused by: java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388) ~[na:1.8.0_40]
at scala.util.Random.nextInt(Random.scala:66) ~[scala-library-2.11.6.jar:na]
The problem is solve, but i am not sure, why this produce, I think there is problem with reactivemongo-extension JSONDao library. because when i use findOne instead of findRandom the code is run successfully, but the findRandom is run good on BSON dao. Still not found what the exact problem is that, but following is the resolved code.
def getStoredAccessToken(authInfo: AuthInfo[User]) = {
println(authInfo.user.email+" ---- "+authInfo.user._id.get)
var future = accessTokenService.findOne($doc("clientId" $eq authInfo.user.email, "userId" $eq authInfo.user._id.get)); //user findOne instead of findRandom in JsonDao
future.map { option => {
if (!option.isEmpty){
var accessToken = option.get;
var value = Crypto.validateToken(accessToken.createdAt.value)
Some(scalaoauth2.provider.AccessToken(accessToken.accessToken, accessToken.refreshToken, authInfo.scope,
Some(value), new Date(accessToken.createdAt.value)))
}else{
Option.empty
}
}}
}
I have been working on this issue for quite a while now and I cannot find a solution...
A web app built with play framework 2.2.1 using h2 db (for dev) and a simple Model package.
I am trying to implement a REST JSON endpoint and the code works... but only once per server instance.
def createOtherModel() = Action(parse.json) {
request =>
request.body \ "name" match {
case _: JsUndefined => BadRequest(Json.obj("error" -> true,
"message" -> "Could not match name =(")).as("application/json")
case name: JsValue =>
request.body \ "value" match {
case _: JsUndefined => BadRequest(Json.obj("error" -> true,
"message" -> "Could not match value =(")).as("application/json")
case value: JsValue =>
// this breaks the secod time
val session = ThinkingSession.dummy
val json = Json.obj(
"content" -> value,
"thinkingSession" -> session.id,
)
)
Ok(Json.obj("content" -> json)).as("application/json")
}
} else {
BadRequest(Json.obj("error" -> true,
"message" -> "Name was not content =(")).as("application/json")
}
}
}
so basically I read the JSON, echo the "value" value, create a model obj and send it's id.
the ThinkingSession.dummy function does this:
def all(): List[ThinkingSession] = {
// Tried explicitly closing connection, no difference
//val conn = DB.getConnection()
//try {
// DB.withConnection { implicit conn =>
// SQL("select * from thinking_session").as(ThinkingSession.DBParser *)
// }
//} finally {
// conn.close()
//}
DB.withConnection { implicit conn =>
SQL("select * from thinking_session").as(ThinkingSession.DBParser *)
}
}
def dummy: ThinkingSession = {
(all() head)
}
So this should do a SELECT * FROM thinking_session, create a model obj list from the result and return the first out of the list.
This works fine the first time after server start but the second time I get a
play.api.Application$$anon$1: Execution exception[[SQLException: Timed out waiting for a free available connection.]]
at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
at scala.Option.map(Option.scala:145) [scala-library.jar:na]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: java.sql.SQLException: Timed out waiting for a free available connection.
at com.jolbox.bonecp.DefaultConnectionStrategy.getConnectionInternal(DefaultConnectionStrategy.java:88) ~[bonecp.jar:na]
at com.jolbox.bonecp.AbstractConnectionStrategy.getConnection(AbstractConnectionStrategy.java:90) ~[bonecp.jar:na]
at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:553) ~[bonecp.jar:na]
at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.java:131) ~[bonecp.jar:na]
at play.api.db.DBApi$class.getConnection(DB.scala:67) ~[play-jdbc_2.10.jar:2.2.1]
at play.api.db.BoneCPApi.getConnection(DB.scala:276) ~[play-jdbc_2.10.jar:2.2.1]
My application.conf (db section)
db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:file:database/[my_db]"
db.default.logStatements=true
db.default.idleConnectionTestPeriod=5 minutes
db.default.connectionTestStatement="SELECT 1"
db.default.maxConnectionAge=0
db.default.connectionTimeout=10000
Initially the only thing set in my config was the connection and the error occurred. I added all the other stuff while reading up on the issue on the web.
What is interesting is that when I use the h2 in memory db it works once after server start and after that it fails. when I use the h2 file system db it only works once, regardless of the server instances.
Can anyone give me some insight on this issue? Have found some stuff on bonecp problem and tried upgrading to 0.8.0-rc1 but nothing changed... I am at a loss =(
Try to set a maxConnectionAge and idle timeout
turns out the error was quite somewhere else... it was a good ol' stack overflow... have not seen one in a long time. I tried down-voting my question but it's not possible^^