Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - NumberFormatException while parsing an int

So I am making file reader/writer that can access a given file and save/read from it.I am having a problem while reading from the file. The contents are integers, string and double separated by "|" delimiters. I am using StringTokenizer to separate the tokens and save them to each individual variable but when I am reading the integers I get a NumberFormatException even though the string contains only an int.

Here is the code:

FileReader fr = new FileReader(filename);
BufferedReader buff = new BufferedReader(fr);
String line;

while ((line = buff.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(line, "|");
    while (st.hasMoreElements()) {
         int Id = Integer.parseInt(st.nextToken());
         String Name = st.nextToken();
         double cordX = Double.parseDouble(st.nextToken());
         double cordY = Double.parseDouble(st.nextToken());
    }
}

An example line of the file :

8502113|Aarau|47.391355|8.051251

And the error :

java.lang.NumberFormatException: For input string: "8502113"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at storage.FileUtilities.readCitiesFromFile(FileUtilities.java:63)
at basics.Test.main(Test.java:16)

Am I missing something here? Is StringTokenizer tampering the string in some kind of way that I don't know?

EDIT: Here is the code that creates the file:

FileWriter fw = new FileWriter(filename, !overwrite); // For FileWriter true = append, false = overwrite, so we flip the value.
    BufferedWriter buff = new BufferedWriter(fw);
    String coordConvertor;

    for (int i = 0; i <= cities.size() - 1; i++) {
        buff.write(Integer.toString(cities.get(i).getId()));
        buff.write("|");
        buff.write(cities.get(i).getName());
        buff.write("|");
        coordConvertor = Double.toString(cities.get(i).getCoord().getX());
        buff.write(coordConvertor);
        buff.write("|");
        coordConvertor = Double.toString(cities.get(i).getCoord().getY());
        buff.write(coordConvertor);
        buff.newLine();
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

There are hidden unicode characters in the String your retrieved with st.nextToken(). Use this code instead to remove them

int Id = Integer.parseInt(st.nextToken().replaceAll("\p{C}", ""));
String Name = st.nextToken().replaceAll("\p{C}", "");
double cordX = Double.parseDouble(st.nextToken().replaceAll("\p{C}", ""));
double cordY = Double.parseDouble(st.nextToken().replaceAll("\p{C}", ""));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...