aspose file tools*
The moose likes Beginning Java and the fly likes string to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string to int" Watch "string to int" New topic
Author

string to int

jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
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!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Have you tried:
new Integer(String);
or
new Float(String);
Rene
[ July 28, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Check out the
indexOf()
method in the String class.

You can check for
indexOf( '-' )
and deal with the negative input if you find one.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
Thanks for the help Rene,
I tried this several different ways and was not able to get it to work.

C:\My Documents\javaps\ajava2\week4\ex\exper\ClockThread.java:49: incompatible types
found : java.lang.Integer
required: int
offset = new Integer(s);

C:\My Documents\javaps\ajava2\week4\ex\exper\ClockThread.java:50: cannot resolve symbol
symbol : method parseInt (java.lang.Integer)
location: class java.lang.Integer
offset = Integer.parseInt(i);
jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
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 '+'?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I've just tried this:

and it works.
PS. a positiv number must be without +
Rene
jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
so would this programming logic make sence:

i'm still not positive on how to use indexOf, but i will go do some experimenting.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

another sample:

Rene
[ July 28, 2002: Message edited by: Rene Larsen ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
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:
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

When you use my sample 'new Integer(String).intValue()' you keep it as a negativ number.
/Rene
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]
jo tay
Greenhorn

Joined: Jul 28, 2002
Posts: 9
thanks Rene !
great link Dirk!
I must say I have learned a good bit more Java today than I have in awhile!
 
Consider Paul's rocket mass heater.
 
subject: string to int
 
Similar Threads
Help With With HeadFirst Java Chap.6
Any API that returns the timezone in GMT(+/-) format?
PlainDocument question
Change Timezone from GMT to EST, the best way to accomplish?
Delete last character appended in TextArea