I have a program that is getting input from a text file. One of the values is in the format of: + or - , a number ex. -1, +4.5, -2.75 these are coming in as strings. when i try to change them to an int, i get an exception. java.lang.NumberFormatException: +1 I need an int to change the hour on a clock: cal.add(Calendar.HOUR, getOffset()); getOffset() returns an int that has been changed from a string in setOffset(). Any ideas? Thanks!
Thanks for the help Marilyn, I looked up the method indexOf() and am a little confused by what index means: "Returns the index within this string of the first occurrence of the specified character. " And when you say, "You can check for indexOf( '-' ) and deal with the negative input if you find one. " deal with it how? I need to differentiate between positive and negative so I may change my clock by that amount. should i try to look for the '+'?
The String::indexOf( char ) method returns the index position of the first occurrence of the character that you are searching for. If the character is not found, then it returns -1. Marilyn was suggesting that if -1 were returned, then you'd know that the string did not contain the specified character.
thanks everyone, i did not end up using indexOf(),, although I do understand it a little more. i still have not figured out how to keep a negative number when changing to an int ,, but here is what i did to avoid the exception:
jo, If you're curious about why an exception is thrown when trying to use Integer::parseInt with a String containing a leading '+' symbol, you may want to take a look at this past conversation on this topic. Good Luck. [ July 28, 2002: Message edited by: Dirk Schreckmann ]