Results 1 to 3 of 3
  1. #1
    Nhatmeister is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default help with Integer.parseInt() pls.

    I am reading a line of numbers from a file, separated by " ". I cannot see why the error when attempt to convert String to Integer.
    This is the code:

    while(inStream.hasNext())
    {
    String line = new String(inStream.nextLine().trim());
    String[] elementInLine = line.split(" ");

    for(int i=0; i<elementInLine.length; i++)
    {
    String x = elementInLine[i];
    int y = Integer.parseInt(x); //error here
    System.out.println(elementInLine[i]);
    }
    }

    this is the error:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "461"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:481)
    at java.lang.Integer.parseInt(Integer.java:514)
    at Humphrey.main(Humphrey.java:54)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,879
    Rep Power
    25

    Default Re: help with Integer.parseInt() pls.

    Check the contents of the String: x. It has a non-ASCII value.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nhatmeister is offline Member
    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    Default Re: help with Integer.parseInt() pls.

    Thanks much...
    did this "string fixed = original.replaceAll("[^\\x20-\\x7e]", ""); //all is well..

    Thanks again.

Similar Threads

  1. Integer.parseInt error help
    By TheElder777 in forum New To Java
    Replies: 7
    Last Post: 10-21-2012, 10:49 AM
  2. Integer parseInt(str) question
    By prasads in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 11:10 PM
  3. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 06:36 AM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •