wood burning stoves*
The moose likes Java in General and the fly likes Problem with parseInt().... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with parseInt()...." Watch "Problem with parseInt()...." New topic
Author

Problem with parseInt()....

thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Hello everybody,
My requirement is that I have to take 2 user inputs-1 timestamp(say in the format yyyy/mm/dd-hh:min:sec) and a zone offset(for example -5:30 hrs) and add the offset to the timestamp to generate a new date.
Accepting the input and performing addition is ok(Calendar add mthd)...but the problem is am taking offset as a string and then using parseInt to convert.So long as i dont use signs,it works...but if my offset is something like -5:30,its throwing the number format exception...can somebody advise me how to proceed??

Thanks and regards,
Thejaswini.
[ November 10, 2005: Message edited by: thejaswini ramesh ]
Hemal Pandya
Greenhorn

Joined: Nov 10, 2005
Posts: 18
Originally posted by thejaswini ramesh:
Hello everybody,
My requirement is that I have to take 2 user inputs-1 timestamp(say in the format yyyy/mm/dd-hh:min:sec) and a zone offset(for example -5:30 hrs) and add the offset to the timestamp to generate a new date.
Accepting the input and performing addition is ok(Calendar add mthd)...but the problem is am taking offset as a string and then using parseInt to convert.So long as i dont use signs,it works...but if my offset is something like -5:30,its throwing the number format exception...can somebody advise me how to proceed??


offset= t.getText();//input in a textfield
System.out.println("the offset is :" + offset);
StringTokenizer st = new StringTokenizer(offset,":");

while(st.hasMoreTokens()) {
String h1 = st.nextToken();
System.out.println("h:" + h1);
hi = Integer.parseInt(h1);//doesn't accept -5:30
System.out.println("int hi:" + hi);

String m1 = st.nextToken();
System.out.println("m:" + m1);
mi = Integer.parseInt(m1);
System.out.println("int mi:" + mi);

Thanks and regards,
Thejaswini.


The String "-5" should can be processed by Integer.parseInt without exception. Can you post the output you get with the above?

But your comment says it doesn't accept "-5:30", which it ceetainly won't. Are you sure the code works when the offset is "5:30" and "8:00"?
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
ParseInt method wont accept 5:30 because it's not a Valid number.

FYI: If you want to convert a time to any specific Time zone you can use the class called "DateFormat" & "TimeZone"
[ November 10, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Srinivasa Raghavan:
FYI: If you want to convert a time to any specific Time zone you can use the class called "DateFormat" & "TimeZone"


Thank you for your advice, but the question is somewhat different.
Suppose you have a string like "2005.11.10 07:48:36-0500". I see how
SimpleDateFormat can parse that to return a Date, but how can one use
(Simple)DateFormat to extract a TimeZone or offset?


There is no emoticon for what I am feeling!
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228

but how can one use
(Simple)DateFormat to extract a TimeZone or offset?

I thought Ramesh needs to get the time in difference time zone hence i added my comments in (FYI). This can be done by dateFormatter.setTimeZone( timeZone obj)
Hemal Pandya
Greenhorn

Joined: Nov 10, 2005
Posts: 18
Originally posted by thejaswini ramesh:
Thanks Hemal and Srinivasa...now it seems negative offsets like -5:30 work for todays date but other inputs it doesn't!!I will paste my entire code with sample output...pls help me analyse.Also can you suggest me a more concise way of implementing this?

Thejaswini



That's too much code to debug. Try to write a small program that demonstrates the problem, e.g. by stripping all swing and input functionality and instead using hard-coded values.

Taking this route my help you solve the problem on your own as well.
thejaswini ramesh
Ranch Hand

Joined: Oct 19, 2005
Posts: 74
Thanks everybody for your valueable suggestions...hemal I did debug by hardcoding it...was able to solve tht...

This is accepting offset input with/without the sign...please see if this can be made more concise:

String offset = "+5:30";
System.out.println("the offset is :" + offset);


StringTokenizer st = new StringTokenizer(offset,":");

try
{
while(st.hasMoreTokens()) {
String h1 = st.nextToken();
System.out.println("h:" + h1);

StringTokenizer s = new StringTokenizer(h1,"+",false);
if(s.hasMoreTokens()) {
String hr = s.nextToken();
hi = Integer.parseInt(hr);
} else
hi =Integer.parseInt(h1);

System.out.println("int hi:" + hi);

String m1 = st.nextToken();
System.out.println("m:" + m1);
mi = Integer.parseInt(m1);
System.out.println("int mi:" + mi);

newCal.add(newCal.HOUR_OF_DAY,hi);
newCal.add(newCal.MINUTE,mi);

Date d2= newCal.getTime();
System.out.println("the modified date is :" + d2);

}
}catch(Exception e) {
System.out.println("exception occured");
}
Hemal Pandya
Greenhorn

Joined: Nov 10, 2005
Posts: 18
Originally posted by thejaswini ramesh:
Thanks everybody for your valueable suggestions...hemal I did debug by hardcoding it...was able to solve tht...

This is accepting offset input with/without the sign...please see if this can be made more concise:

String offset = "+5:30";
System.out.println("the offset is :" + offset);
StringTokenizer st = new StringTokenizer(offset,":");
[snip]
}


See String.split(), try offset.split(":");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with parseInt()....
 
Similar Threads
Can I use String.format?
Overloded parseInt(String, int)?
Time-Date Conversion??
Handling a strange date format
Problem with JFileChooser