PDA

View Full Version : Java programing question


BlueFang08
04-13-2004, 03:19 PM
Okay here is my problem I am readin in a date but when I read it in it come in as a string.

I was wondering how to change a String into and Int?

Thanks
brad...

wild.irish
04-13-2004, 03:38 PM
i vaguely remember (haven't touch java for like 4 years), but can't you just use Date class with a string?

BlueFang08
04-13-2004, 03:45 PM
Hmm possibly but i am reading this date in from a file so it just assumes its a string i think.

any ideas?

wild.irish
04-13-2004, 04:16 PM
check out:
http://java.sun.com/docs/books/tutorial/java/data/andback.html
i couldn't really find anything with Date class.. but you could probably do something once you have numbers instead of string.

DrJones
04-13-2004, 06:16 PM
I was wondering how to change a String into and Int?



Assuming that you just have a string that is only a string of an integer, ie "15325" and not something like "Day=12532" you can do it using the static parseInt method of Integer. Just like:

int foo = Integer.parseInt("12345");


parseInt
public static int parseInt(String s)
throws NumberFormatExceptionParses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

DrJones
04-15-2004, 12:46 AM
Did you figure it out?

davidm_sh
04-15-2004, 07:49 AM
http://java.sun.com/j2se/1.3/docs/api/java/lang/Integer.html

Why not just create a new Integer variable and with the declaration pass it the string you want to convert? Look in the constructor summary

Example:

Integer foo = new Integer(<string variable here>);

Just a thought. It has been almost 1.5 years since I have done any Java programming. From there you could go to a literal int with .intValue() if you wanted.

BlueFang08
04-17-2004, 10:57 PM
Did you figure it out?

yup got it working thanks dood.