在Java中将java.lang.object转换为int

时间:2017-03-30 12:49:57

标签: java

我有一个返回java.lang.object类型对象的方法,我想将其转换为int。我试过这个:

oldComment.get("count"); 

返回java.lang.object,我想将其转换为int。我试过了:

(Integer)oldComment.get("count");
Integer.valueOf(oldComment.get("count"));
Integer.parseInt(oldComment.get("count"));


con = Base.connection();
String query = "UPDATE COMMENT SET LIKES = ? WHERE POST_ID = ?";
PreparedStatement pst = con.prepareStatement(query);
pst.setObject(1, (Integer.parseInt(oldComment.get("likes").toString())) + Integer.valueOf(rateComment.getCount()));
pst.setString(2, rateComment.getPost_id());
int k = pst.executeUpdate();

这是导致问题的代码(Integer.parseInt(oldComment.get("likes").toString()))

错误Stacktrace:

[qtp25844331-17] ERROR spark.http.matching.GeneralError -
java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.lang.Integer.parseInt(Integer.java:592)
        at java.lang.Integer.parseInt(Integer.java:615)
        at com.soul.seeker.serviceImpl.CommentRatingServiceImpl.rateComment(CommentRatingServiceImpl.java:50)
        at com.soul.seeker.Application.lambda$main$12(Application.java:161)
        at spark.ResponseTransformerRouteImpl$1.handle(ResponseTransformerRouteImpl.java:47)
        at spark.http.matching.Routes.execute(Routes.java:61)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:130)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
        at org.eclipse.jetty.server.Server.handle(Server.java:517)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:308)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
        at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
        at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
        at java.lang.Thread.run(Thread.java:745)

以上都没有奏效。我如何转换它?

1 个答案:

答案 0 :(得分:1)

首先,你不应该回归java.util.Object,这是一个非常坏的习惯。如果您的值是数字,则应返回java.lang.Number。如果它是一个字符串,您应该返回java.lang.String

如果您没有选择权,可以使用以下代码进行转换:

// This method can throw NumberFormatException, catch it if you want
public Integer toInt(Object obj) {
    // Use intValue on a Number to improve performance
    if(obj instanceof Number) {
         return ((Number) obj).intValue();
    }

    return Integer.parseInt(obj.toString());
}

编辑:在你的堆栈跟踪中,你的程序试图解析一个空字符串,因此它抛出一个NumberFormatException,你应该抓住它。