ParseLong引发NumberFormatException [英] ParseLong raise NumberFormatException

查看:189
本文介绍了ParseLong引发NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 java.lang.NumberFormatException: For input string: "10"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:441)

相关代码段:

  public static class NodeWritable implements Writable {

    public double msg;
    public double rank;
    public String others;

    public NodeWritable(double msg, double rank, String others) {
      this.msg = msg;
      this.rank = rank;
      this.others = others;
    }

    public NodeWritable() {
      this.msg = 0.0;
      this.rank = 0.0;
      this.others = "";
    }

    @Override
    public void write(DataOutput out) throws IOException {
      out.writeDouble(msg);
      out.writeDouble(rank);
      out.writeChars(others + "\n");
    }

    @Override
    public void readFields(DataInput in) throws IOException {
      msg = in.readDouble();
      rank = in.readDouble();
      others = in.readLine();
    }

    @Override
    public String toString() {
      return "" + rank;
    }
  }


  ArrayList<Long> incoming_vids = new ArrayList<Long>();
  for (NodeWritable msg : messages) {
    String in_vid = msg.others.trim();
    incoming_vids.add(Long.parseLong(in_vid));
  }

这怎么会发生?我已经与Google进行了一些研究.有时 NumberFormatException 似乎是由大数字引起的.但是我只是无法为我的情况做一个解释.

How can this happen? I've done some research with Google. Sometime NumberFormatException seems to be caused by big numbers. But I just can't find a possible explanation for my case.

推荐答案

您可以使用此

    for(int i=0;i<in_vid.length();i++) {
            char ch = in_vid.charAt(i);
       if( Character.isDigit(ch)) {
//   do something
}
    }

如果不是数字,则可以在循环中将其消除,并仅传递具有数字的字符串.

if its other than digit then you can eliminate it in the loop and pass only the string which has digits.

这篇关于ParseLong引发NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆