This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

decimal to binary number converter

 
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.
 
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.
 
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.
 
    Bookmark Topic Watch Topic
  • New Topic