Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Converting string to int in java

Options
  • 15-12-2005 7:19pm
    #1
    Registered Users Posts: 9,225 ✭✭✭


    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 Posts: 9,225 ✭✭✭Chardee MacDennis


    nope that wont work:

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


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


    post your code.


  • Registered Users Posts: 9,225 ✭✭✭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 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 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 Posts: 9,225 ✭✭✭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 Posts: 7,500 ✭✭✭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 Posts: 4,003 ✭✭✭rsynnott




Advertisement