2012-02-11 96 views
1

我們的應用程序是基於Scala的,基於Play!框架。我使用Scala Random生成了一個隨機數。這是用作我們應用中每個帳戶的唯一密鑰。將字符串轉換爲長字符時出現Scala NumberFormatException?

然而,當我去到新帳戶保存到數據庫中,它拋出一個java.lang.NumberFormatException

更多信息:我的帳戶ID的字符串轉換爲斯卡拉龍。我正在使用Squeryl對象查找它,獲取ID,然後將其轉換。這裏是什麼樣子:

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

這是findAccountByUnique樣子:

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

上錯誤的堆棧跟蹤:

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/)。 – 2012-02-11 08:42:45

+0

你需要什麼信息?既然你的評論,我甚至用java.UUID(非常獨特)嘗試它,它會引發相同的錯誤。 – crockpotveggies 2012-02-11 08:51:31

+0

*「您還需要什麼信息?」* S-C-C-E的哪一部分難以理解? – 2012-02-11 08:56:18

回答

0

經過進一步調查,它看起來像是有數據庫幫助程序返回垃圾數據。實際上它實際上是返回它自己的「樣本」數據,它恰好是一個十六進制數據。這是Squeryl的一個圖書館,看起來某個地方有些不正常,因此引發了「樣本」反應。

看起來像因爲account.uniqueKey已經從先前的查詢更新,它帶來了一些垃圾。仍然在研究它是如何發生的,但至少我發現了真正的問題。

0

編號是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)。longlong類型不接受參數' – crockpotveggies 2012-02-11 09:34:47

+0

我說魔術的原因是,如果我刪除整條線,當我訪問account.id時突然我有權訪問正確的帳戶ID。它必須是Squeryl的「超級對象」,在某處做某事,因爲沒有邏輯代碼在帳戶對象中查找關鍵字。 – crockpotveggies 2012-02-11 09:37:26

+0

我曾推測,因爲long可能使用java的parseLong(),那麼會存在toLong(int base)。對不起 – 2012-02-11 10:11:55

4

爲十六進制數轉換爲十進制一個有java.lang.Long.parseLong

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

看起來這會做詭計,我唯一擔心的是爲什麼要返回一個十六進制數?這是一個最大ID爲14的測試數據庫。謝謝! – crockpotveggies 2012-02-11 19:11:23

+0

@DeLonge:你問了一種將字符串轉換爲十六進制數的方法,我向你展示了一種方法。如果你有進一步的要求,你必須告訴他們。 – sschaef 2012-02-12 08:43:35

+0

情況並非如此。 Joop的回答指出了十六進制問題。問題總是在不斷髮展,如果你不能再評論,請親切地說。 – crockpotveggies 2012-02-12 18:57:46