0

I am trying to assign a string value from an array to an integer via parseInt and I'm getting this error. Any suggestions?

System.out.println("in the loop for loading info");

try
{
    String testnumber = "1  ";
    System.out.println("Numeric value of testnumber is:" + Integer.parseInt(testnumber.trim()));
    System.out.println("String value from array is:" + Global.teamPlayerHandicap[row][0].trim());

    int testvalue = Integer.parseInt(Global.teamPlayerHandicap[row][0].trim());
    System.out.println("Test value:" + testvalue);
}
catch (NumberFormatException e)
{
    System.out.println("This is not a number:" + Global.teamPlayerHandicap[row][0].trim() +"END");
    System.out.println(e.getMessage());
}

Output:

in the loop for loading info

Numeric value of testnumber is:1

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1"

String value from array is:1

This is not a number:1END

For input string: "1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at com.snafilter.TGL.DoWorkTGL1.PopulateSubmitScoresInfo(DoWorkTGL1.java:1271)
Daniel
  • 1,920
  • 4
  • 17
  • 35
Snay
  • 15
  • 1
  • 2
  • 6
  • 2
    Is there something weird going on with threads? It looks like this code is working fine, but another thread is throwing the error. Is the code you've given definitely around line 1271 of DoWorkTGL1.java? – gandaliter May 05 '14 at 19:10
  • 1
    Try replacing `.trim()` by `.replaceAll("[^\\d]","")` and run again. – elias May 05 '14 at 19:10
  • Holy cow...the replaceAll returns the actual value in "Test value:". So would it be that some hidden stuff is loaded into my array and I trim is not getting rid of it? – Snay May 05 '14 at 19:14

1 Answers1

0

Try with specified character encoding

String trimStr = Global.teamPlayerHandicap[row][0].trim();
byte[] bytes = trimStr.getBytes();
String str = new String(bytes, Charset.forName("UTF-8"));

int testvalue = Integer.parseInt(str);
System.out.println(testvalue);

For more info have a look at NumberFormatException error (parseInt)

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76