• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

string to int

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried:
new Integer(String);
or
new Float(String);
Rene
[ July 28, 2002: Message edited by: Rene Larsen ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the
indexOf()
method in the String class.

You can check for
indexOf( '-' )
and deal with the negative input if you find one.
 
jo tay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just tried this:

and it works.
PS. a positiv number must be without +
Rene
 
jo tay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another sample:

Rene
[ July 28, 2002: Message edited by: Rene Larsen ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
jo tay
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use my sample 'new Integer(String).intValue()' you keep it as a negativ number.
/Rene
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rene !
great link Dirk!
I must say I have learned a good bit more Java today than I have in awhile!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic