java - Converting strings to integers from an array of strings -
i'm trying convert strings array integers using parseint(). reading in lines many separate files this:
car,house,548544587,645871266
i have code below:
string [] tokens = line.split(","); try { line = line.trim(); int = integer.parseint(tokens[2]); int b = integer.parseint(tokens[3]); int c = (b - a); system.out.println(c); } catch (numberformatexception e) { system.out.println(e); }
but fails errors this, each line read in:
java.lang.numberformatexception: input string: "548544587" java.lang.numberformatexception: input string: "645871266"
any idea might missing?
you need remove quotes before splitting. fails convert "number"
actual number because of quotes.
string line = "\"car\",\"house\",\"548544587\",\"645871266\""; string[] tokens = line.replace("\"", "").split(","); try { int = integer.parseint(tokens[2]); int b = integer.parseint(tokens[3]); int c = (b - a); system.out.println(c); } catch (numberformatexception e) { system.out.println(e); }
output:
97326679
Comments
Post a Comment