Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

java - ParseLong raise NumberFormatException

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

Relevant segment of code:

  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 + "
");
    }

    @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));
  }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can loop on the string in_vid and check if any character is other than digit using this

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...