• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculations in a string in Java

 
Greenhorn
Posts: 8
C++ Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this code to translate 12 hour time to 24 hour time. Inputs will be in the format HH:MM:SSAM or HH:MM:SSPM. Outputs will be in the form HH:MM:SS. I have this code but I can't print the preceding 0 for inputs like 12:04:22AM. My output comes as 0:04:22. Please help me correct this.

Note: I know that there exists better ways (e.g. String.format()) to solve this problem but I need to know this answer.

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

there exists better ways (e.g. String.format()) to solve this problem


Yes that method would do what you want.  Look at the Formatter class's API doc to see how to build a format string for what you want.
http://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does 48 mean? That is a confusing “magic number”. If you come back in three months you will not know what it means at all. If you mean to subtract the ASCII value of 0, do just that.
H - '0'
 
Somnath Rakshit
Greenhorn
Posts: 8
C++ Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I just have the answer to the question I asked? It seems like I'm getting to know everything else apart from the question I asked.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for what exactly to put in your formatter, you aren't going to get that here. We prefer not to just give out answers, we're more of a learning site. So Norm suggested a Formatter: start with that and if you can't make it work post back with a description of where you are stuck.
 
Somnath Rakshit
Greenhorn
Posts: 8
C++ Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know the use of a formatter. All I want to know is that why am I getting that kind of an output for the input I provided? Why am I not getting a preceding 0?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Two points. One. The plus operator is overloaded to either do mathematical addition, or string concatenation. Two. The plus operator only works on two operands at a time -- and have an associativity of left to right.

So, what do you think happens when it tries to adds the first two operands (H+h+":"+M+m+":"+S+s) , which are both primitive ints?

Henry
 
Somnath Rakshit
Greenhorn
Posts: 8
C++ Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But then how come it works for the minute and seconds part and not the hour part?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Somnath Rakshit wrote:But then how come it works for the minute and seconds part and not the hour part?



Check out the "left associative" part of what Henry said. How does that make the hours number act differently than the minutes number?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Likes 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Somnath Rakshit wrote:Can I just have the answer to the question I asked? . . .

If you take your car to a garage to have the spark plugs replaced and the mechanic notices that your tyres are worn to the stage where they would be dangerous for you to drive on, you would expect him to tell you.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
Check out the "left associative" part of what Henry said. How does that make the hours number act differently than the minutes number?



Interestingly, while the ranch does indeed gets a lot of questions regarding "precedence", and mainly due to confusing it with "evaluation order", the concept of "associativity" rarely comes up. This is weird because the concept of associativity is definitely much closer related to precedence than evaluation.

Anyway, to give an example... take this simple expression...

4 + 3 + 2 + 1

All the operators are the same, so same precedence. Since the plus operator is left associative (and order of evaluation is left to right), it is actually easily envisioned as ...



Did you happen to notice that, with the exception of the first two operands, no other operand was operated on a neighboring operand?

Henry
 
Somnath Rakshit
Greenhorn
Posts: 8
C++ Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is such a brilliant answer. Thanks a ton!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic