Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Converting string to int in java

  • 15-12-2005 07:19PM
    #1
    Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭


    does anyone know how to convert a string of value -42 ,for example, to an int. i cant seem to parse negative numbers stored as strings.

    thanks


Comments

  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    int num = Integer.parseInt(stringNum);


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    nope that wont work:

    Exception in thread "main" java.lang.NumberFormatException: null


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    post your code.


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    excuse the stupid variable names - i was getting very board...


    int[][] guvnor = new int[5][100];
    String cheese;
    int rabbite;
    String delim = "-";
    for(int nerves = 0; nerves < fancy.length; nerves++){
    for(int megamac = 0; megamac < 100; megamac++){
    cheese = pants[nerves][megamac];
    rabbite = Integer.parseInt(cheese);
    guvnor[nerves][megamac] = rabbite;
    }
    }


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    Any change the input string turns out to be "-42"? because parse isnt going to figure out how "-" is a number....


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    What is "fancy" declared as?

    Nothing there will ever be negative. Your null error message what line is that appearing on?


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    parseInt() will handle a negative sign as the first char fine.

    The current problem (Exception in thread "main" java.lang.NumberFormatException: null) is most likely caused by sending a null String to parseInt - parseInt(null)

    An element of the pants[][] array (presumably String[][] ) is null

    You are iterating thought the pants[][] array, but using bounds of fancy.length and 100 - Make sure these are correct!


  • Registered Users, Registered Users 2 Posts: 9,228 ✭✭✭Chardee MacDennis


    nice one ph this makes sense so if i throw in an if statement saying it should do nothing if the value is null?

    EDIT: yup this is working, thanks guys!!


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    I have to say one thing, and one thing only.

    Kudos!

    I've never seen such wonderfully named variables in my life! :p


  • Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭BrokenArrows


    i did that once for a fairly big program. All the names were all over the place. I left the project for about 3 weeks and when i came back to it i had no idea what anything was.

    Took a while to sort out.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 4,003 ✭✭✭rsynnott




Advertisement