java - How to parse String containing '+' symbol -


this question has answer here:

i have string has numbers.

i have code this,

    string tempstr = "+123";     numberformat numformat = numberformat.getnumberinstance();     numformat.setparseintegeronly(true);     try {         int ageindays = integer.parseint(tempstr);         system.out.println("the age in days "+ageindays);     } catch (numberformatexception e) {         // todo auto-generated catch block         e.printstacktrace();     } } 

it working fine number negative symbols.but can't number '+' symbol. throws numberformatexception.

so how can parse number '+' symbol string?

in case using java 7 or higher recommend using java.lang.long; or java.lang.integer; (if sure have parse integers).

it works both cases.

later edit: didn't see using integer.parseint();

therefore assume have java 6 or less.

in case try calling following method before line integer.parseint(tempstr); remove + in case exists:

private static string removeplussignifexists(string numberasstring) {     if(numberasstring == null || numberasstring.length() == 0){         return numberasstring;     }      if(numberasstring.charat(0) == '+'){         return numberasstring.substring(1, numberasstring.length());     } else {         return numberasstring;     } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -