This discussion is archived
13 Replies Latest reply: Mar 22, 2013 4:42 PM by 979857 RSS

NumberFormatException: For input string: "1"

979857 Newbie
Currently Being Moderated
I'm trying to load data from a file to TimesTen using JDBC but NumberFormatException error occurs.

This is how my table looks like:

String tab_wysz = "CREATE TABLE WYSZ(CompanyID TT_INTEGER NOT NULL, ClientID TT_INTEGER NOT NULL, SearchDate DATE NOT NULL, Primary Key(CompanyID, ClientID))";

How I load data (the same as in the sample TT programs):

String INPUTFILEWk = "/book/wysz.txt";
String INSERTWysz = "insert into Wysz values (?,?,?)";

PreparedStatement pInsOrd = ttcon.prepareStatement(INSERTWysz);

BufferedReader in = new BufferedReader(new FileReader(plik));
               String line;
                                                       
               try {
                    //in = new BufferedReader(new FileReader(INPUTFILEWk));
                    while ((line = in.readLine()) != null) {
                         // Skip comments
                         if (line.charAt(0) == '#') {
                         continue;
                         }
                         outStream.println("Processing data: " +line);
                         String[] fields = line.split(";");

pInsOrd.setInt(0, Integer.parseInt(fields[0])); // CompanyID
                         pInsOrd.setInt(1, Integer.parseInt(fields[1])); // ClientID
                         pInsOrd.setDate(2, Date.valueOf(fields[2])); // SearchDate

pInsOrd.executeUpdate();

                         reportSQLWarnings(pInsOrd.getWarnings());
                         // }
                         ttcon.commit();
                    }
                    in.close();
                    pInsOrd.close();
               } catch (IOException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                    }
               catch(SQLException e) {
                    e.printStackTrace();
               } catch (NumberFormatException e1) {
                         // TODO Auto-generated catch block
                         e1.printStackTrace();
               }
                         
}

File wysz.txt looks like this:

1;88894;2011-05-17
28;30799;2011-07-31
28;48583;2012-02-13
28;116684;2008-09-23
28;69301;2009-08-02
28;64609;2007-09-12
83;6514;2007-04-29
83;98420;2008-06-17
83;40325;2008-09-14

The file is in UTF-8 encoding, as well as Eclipse which I use to connect database and load data.

The error looks like this:

Connected to the database

Processing data: 1;88894;2011-05-17
java.lang.NumberFormatException: For input string: "1"
     at java.lang.NumberFormatException.forInputString(Unknown Source)
     at java.lang.Integer.parseInt(Unknown Source)
     at java.lang.Integer.parseInt(Unknown Source)
     at bazy.operacjaInsertILoad(bazy.java:157)
     at bazy_zapytania.testInsertInto(bazy_zapytania.java:114)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Closing the connection



Any sugestion what is wrong ?
  • 1. Re: NumberFormatException: For input string: "1"
    Joe Weinstein Expert
    Currently Being Moderated
    The setInt(0,...) is the problem. JDBC says to start the parameter counting at 1, not zero.
  • 2. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    I changed setInt(0, ...) to

    pInsOrd.setInt(1, Integer.parseInt(fields[0])); // CompanyID
    pInsOrd.setInt(2, Integer.parseInt(fields[1])); // ClientID
    pInsOrd.setDate(3, Date.valueOf(fields[2])); // SearchDate

    But the same error occurs: java.lang.NumberFormatException: For input string: "1"
  • 3. Re: NumberFormatException: For input string: "1"
    Joe Weinstein Expert
    Currently Being Moderated
    Good luck, but parseInt() failing is not a JDBC issue... Odd though...
    Oh, but wait, maybe it is. The setInt() call takes an int, not an Integer,
    so try this:

    pInsOrd.setInt(1, Integer.parseInt(fields[0]).intValue()); // CompanyID
    pInsOrd.setInt(2, Integer.parseInt(fields[1]).intValue()); // ClientID
    pInsOrd.setDate(3, Date.valueOf(fields[2])); // SearchDate
  • 4. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    When I try to put intValue() I receive this error: "Cannot invoke intValue() on the primitive type int"
  • 5. Re: NumberFormatException: For input string: "1"
    Joe Weinstein Expert
    Currently Being Moderated
    My mistake, sorry. ParseInt() returns an int, not an Integer.
    Let's get JDBC out of the picture. Do this:

    int val1 = Integer.parseInt(fields[0]);
    int val2 = Integer.parseInt(fields[1]);

    If that doesn't work, you have a charset/locale/JVM issue, not JDBC.
  • 6. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    It didn't help, still the same problem. Should I create new thread in "Java programming" section or maybe there is some way to move this one there?
  • 7. Re: NumberFormatException: For input string: "1"
    rp0428 Guru
    Currently Being Moderated
    >
    It didn't help, still the same problem. Should I create new thread in "Java programming" section or maybe there is some way to move this one there?
    >
    Why are you debugging this the hard way?

    Use a GUI like NetBeans, set a breakpoint and examine the variables that are being used.
    String[] fields = line.split(";");
    
    pInsOrd.setInt(0, Integer.parseInt(fields[0])); // CompanyID
    pInsOrd.setInt(1, Integer.parseInt(fields[1])); // ClientID
    pInsOrd.setDate(2, Date.valueOf(fields[2])); // SearchDate
    
    pInsOrd.executeUpdate();
    Set a breakpoint after the 'line.split' and then examine what the fields array is holding.

    Then use the 'evaluate expression' window to manually evaluate 'Integer.parseInt(fields[0]) and see what you get.

    You need to quit guessing and just take a look at what is happening in the code.
  • 8. Re: NumberFormatException: For input string: "1"
    EJP Guru
    Currently Being Moderated
    Moderator action: deleted your duplicate thread.
  • 9. Re: NumberFormatException: For input string: "1"
    Joe Weinstein Expert
    Currently Being Moderated
    I just cut-n-pasted the error message into a text box:

    "1"

    and in a simple text box, there is a non-printing character in front of the 1:
    "?1"
  • 10. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    Set a breakpoint after the 'line.split' and then examine what the fields array is holding.
    Then use the 'evaluate expression' window to manually evaluate 'Integer.parseInt(fields[0]) and see what you get.
    Fields array is holding correct values: 0=1, 1=88894, 2=2011-05-17

    After evaluating expression manually it gives me correct result, which is 1. I also checked the second array field, with correct result.
    I just cut-n-pasted the error message into a text box:
    "1"
    and in a simple text box, there is a non-printing character in front of the 1:
    "?1"
    I opened my file with the notepad++ and mark the option to show all hidden characters and there is none, just 1;88894;2011-05-17

    Edited by: 976854 on 2013-03-22 16:03
  • 11. Re: NumberFormatException: For input string: "1"
    baftos Expert
    Currently Being Moderated
    Copy this line from your original post:

    java.lang.NumberFormatException: For input string: "1"


    Open a command prompt and paste it there. There is something before the 1.

    Edit:
    Paste it in notepad. Try to save. It will tell you that you lose information if you save in ANSI and suggest you use Unicode. Do it.
    With a hex editor, you will see an FFFE just before the 1.

    According to http://en.wikipedia.org/wiki/Unicode, FFFE "conveys the zero-width no-break space (a character with no appearance and no effect other than [...]", whatever this means.

    Edited by: baftos on Mar 22, 2013 7:20 PM
  • 12. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    Yes, you are right. I opened a file with cmd and there are some characters in front of 1.

    Edited by: 976854 on 2013-03-22 16:32
  • 13. Re: NumberFormatException: For input string: "1"
    979857 Newbie
    Currently Being Moderated
    baftos wrote:
    Copy this line from your original post:

    java.lang.NumberFormatException: For input string: "1"


    Open a command prompt and paste it there. There is something before the 1.

    Edit:
    Paste it in notepad. Try to save. It will tell you that you lose information if you save in ANSI and suggest you use Unicode. Do it.
    With a hex editor, you will see an FFFE just before the 1.

    According to http://en.wikipedia.org/wiki/Unicode, FFFE "conveys the zero-width no-break space (a character with no appearance and no effect other than [...]", whatever this means.

    Edited by: baftos on Mar 22, 2013 7:20 PM
    I've used WinVi editor and in ANSI I could see hidden characters. I delete them, saved and run my java code again and IT WORKED!

    Thank you very much for all your help and all respond!

    Edited by: 976854 on 2013-03-22 16:42

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points