Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

decimal to binary number converter

 
Beatrice Solano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I want to construct an applet using JBuilder 4 that can convert decimal numbers into binary numbers and vice-versa. I have been using the following code that I'm able to compile without errors but it doesn't work. Can anyone tell me what I'm doing wrong? Thank you in advance and forever.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!
First: Please review The JavaRanch Naming Policy and change your display name.
Second: This code won't compile. So, let's review some of the most apparent errors:

After these corrections are made, and the program will compile, what error messages are you seeing? These often provide very useful information regarding a problem.
Also, I'd recommend a review of Sun's Code Conventions for the Java Language.
Good Luck.
[ April 18, 2002: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the corrections that will allow the program to compile, but it still won't work as I discuss in one of the comments:

Good Luck.
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Dividing the number by two won't get the job done. You will be better off starting from the formula

2^n = #
Knowing this we can easily see that a loop such as:

will fill in the StringBuffer with the correct number of ones and zeros. To fit this into your code:

The reverse is left to the student ...
Regards,
Manfred.
 
Beatrice Solano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys are amazing! I have to admit that when I posted my problem I thought that no one would reply but I couldn't have been more wrong. Learning Java has been a nightmare for me(as you can tell from my lousy coding). I was about to give up on it but the information and people on this site are so helpful that I'm going to stick with it. I will try out the suggested codes. To all those who posted, thanks a bunch!
 
Beatrice Solano
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!
I just wanted to let you all know that I was finally able to get my decimal to binary converter applet to work! Hooray! I couldn't have done it without a lot of help and although I still dislike Java I would like to share the code, just in case someone else is suffering with this problem. Thanks again to Mr. Schreckman and Mr. Leonhardt for your posts. Here's the correct code:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"dislike Java" - huh? Nah.
Good Job.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic