gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:50 28 4
gpt4 key购买 nike

我有一个返回 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()))

错误堆栈跟踪:

[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)

以上均无效。我如何转换它?

最佳答案

首先,你不应该返回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,您应该捕获它。

关于java - 在 Java 中将 java.lang.object 转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43118383/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com