2012-02-11 20 views
1

私たちのアプリはScalaベースで、Play!フレームワーク。 Scala Randomを使用して乱数を生成しました。これは、アプリの各アカウントのユニークなキーとして使用することです。文字列をLongに変換するときのScala NumberFormatException?

私は、データベースに新しいアカウントを保存するために行くときしかし、それはjava.lang.NumberFormatExceptionをスロー:

詳細:私はScalaのロングにアカウントIDの文字列を変換しています。私はSquerylオブジェクトを使ってそれを探してIDをつかんで変換しています。

def findAccountByUnique(criteria: String) = { 
    from(DB.accounts)(a => 
     where(a.uniqueKey == criteria) 
     select (a)) 
    } 

エラーのスタックトレースを::これはfindAccountByUniqueのように見えるものである

val account_id = Account.findAccountByUnique(account.uniqueKey).id.toLong

java.lang.NumberFormatException: For input string: "468b68c" 
     at java.lang.NumberFormatException.forInputString(Unknown Source) 
     at java.lang.Long.parseLong(Unknown Source) 
     at java.lang.Long.parseLong(Unknown Source) 
     at scala.collection.immutable.StringLike$class.toLong(StringLike.scala:209) 
     at scala.collection.immutable.StringOps.toLong(StringOps.scala:31) 
     at controllers.Accounts$.save(Accounts.scala:44) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:548) 
     at play.mvc.ActionInvoker.invoke(ActionInvoker.java:502) 
     at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:496) 
     at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:473) 
     at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161) 
     at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:257) 
     at play.Invoker$Invocation.run(Invoker.java:278) 
     at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:235) 
     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) 
     at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) 
     at java.util.concurrent.FutureTask.run(Unknown Source) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) 
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) 
     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
     at java.lang.Thread.run(Unknown Source) 

私はのように一意のキーを型変換しましたが、ここでは次のようになります。 Scala LongStringの両方が同じエラーをスローします。修正に関する考えはありますか?

+0

一連の乱数を取得した場合、必ずしも一意になるとは限りません。もっと早く助けを求めるには、[SSCCE](http://sscce.org/)を投稿してください。 –

+0

さらに多くの情報が必要ですか?あなたのコメントから、私はjava.UUID(非常にユニーク)でそれを試してみても、同じエラーをスローします。 – crockpotveggies

+0

*「さらに多くの情報が必要ですか?」* S-S-C-C-Eのどの部分を理解しづらいですか? –

答えて

0

さらに調査した結果、それがあったようですジャンクデータを返すデータベースヘルパー。基本的には、実際には16進数である「サンプル」データを返すことになりました。それはSquerylのlibで、どこかで何かが不正な形になっているように見えて、 "サンプル"応答を引き起こしました。

account.uniqueKeyが以前のクエリから更新されているように見えますが、それはいくつかの迷惑メールを持ってきました。それがどうなったのかまだ調べていますが、少なくとも私は本当の問題を発見しました。

0

Idはあなたがjava.lang.Long.parseLongあり、小数点1に16進数を変換するには

val account_id = Account.findAccountByUnique(account.uniqueKey).id.toLong(16) 
+0

残念ながら、コンパイラエラーが発生しました。しかし、Scalaの「魔法」が起こっていると思います。 'scala.this.Predef.augmentString(org.squeryl.PrimitiveTypeMode.singleColumnQuery2RightHandSideOfIn [models.Account](models.Account.findAccountByUnique(account.uniqueCode))。id)。to long of long型は引数を取らない ' – crockpotveggies

+0

なぜ私は魔法を言うのですか?その行全体を削除すると、account.idにアクセスすると突然私は適切なアカウントIDにアクセスできます。どこかで何かをやっているSquerylの "スーパーオブジェクト"でなければなりません。なぜなら、アカウントオブジェクトのキーを調べる論理的なコードがないからです。 – crockpotveggies

+0

私はtoLongがおそらくjavaのparseLong()を使用するため、toLong(int base)が存在すると推定しました。申し訳ありません –

4

を必要とするので、進数である:進10進数に変換する

scala> import java.lang.{ Long => JLong } 
import java.lang.{Long=>JLong} 

scala> JLong.parseLong("468b68c", 16) 
res8: Long = 73971340 

もう一つの方法は、書くことです独自の方法:

def toHex(s: String): Long = { 
    val Hex = "([0-9a-fA-F]+)".r 
    s match { 
    case Hex(_) => java.lang.Long.parseLong(s, 16) 
    case _ => throw new NumberFormatException("invalid hex number: " + s) 
    } 
} 
+0

私はこれがトリックを行うように見えます、私の唯一の懸念はなぜ16進数が返されているのですか?これは、最大IDが14のテストDBです。ありがとうございます! – crockpotveggies

+0

@DeLonge:文字列を16進数に変換する方法を尋ねたところ、私は方法を示しました。さらなる要件がある場合は、それらに伝える必要があります。 – sschaef

+0

これはまったくそうではありませんでした。それは16進問題を指摘したJoopの答えでした。問題は常に進化しています。それ以上のコメントができない場合は、親切に言ってください。 – crockpotveggies

関連する問題