This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes doubt in a program of convert decimal to Hexadecimal-please clarify 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 » Beginning Java
Bookmark "doubt in a program of convert decimal to Hexadecimal-please clarify " Watch "doubt in a program of convert decimal to Hexadecimal-please clarify " New topic
Author

doubt in a program of convert decimal to Hexadecimal-please clarify

Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Hello,

i am making a program of convert decimal to Hexadecimal. please write what is difference between these programs.




output is: 38



output is: 8


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

please write what is difference between these programs


In the first case, your do-loop terminates prematurely, and you have code following it that fixes it. In the second case, your do-loop terminates prematurely, and you don't have code following it that fixes it.

IMO, neither case is optimal. Maybe it would be better to fix the do-loop.



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40


Another suggestion, instead of generating the output string backwards -- requiring the for-loop to reverse it back -- maybe it would be better to generate it in a forward direction. Meaning, instead of this...



How about this...



Henry
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Dear Sir,

i am asking in first cast output is: 38 and in the second case output is: 8

when i use that code in a loop


why again write thses outside the loop
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

why again write thses outside the loop


As mentioned, the code outside of the loop is needed because your loop terminated prematurely. It does one more iteration of the loop.

My suggestion is to get rid of that code, and fix the do-loop so that it doesn't terminate prematurely.

Henry
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Dear Sir,

thank you for your suggestion.
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Dear Sir,

Can you give some light on fix the do-loop so that it doesn't terminate prematurely.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

Babu lal wrote:Dear Sir,

Can you give some light on fix the do-loop so that it doesn't terminate prematurely.


I showed you the fix, in my first response (not tested, of course).

Henry
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Babu Singh
Ranch Hand

Joined: Aug 17, 2009
Posts: 72
Dear Sir,

Now tested.



Thanks a lot.
 
Consider Paul's rocket mass heater.
 
subject: doubt in a program of convert decimal to Hexadecimal-please clarify
 
Similar Threads
missing return statement?
ASCII TO EBCDIC conversion preserving COMP-3
Evaluating Postfix Expression
hexadecimal numbers
help: java program to convert decimal number to hexadecimal number