473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

decimal to binary converter(java)

Hi. I have a problem with my converter. It works fine. the only problem is that it doesnt convert single digit numbers properly.
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.  
  4.     /**
  5.      * @param args the command line arguments
  6.      */
  7.         public static void main(String[] args){  //throws IOException
  8.         // TO DO codestatic double toDecimal (String s)
  9.            int dec;
  10.            String bin, conv;
  11.                     int a=1;
  12.                     while (a==1)
  13.                     {
  14.            //BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  15.            Scanner scan = new Scanner(System.in);
  16.  
  17.            System.out.println("Please enter decimal number to convert to binary" + "\n" + "maximum of 10 digits please");
  18.            bin = scan.next();
  19.            dec = Integer.parseInt(bin);
  20.            int max[] = new int[10];
  21.            int i = 0;
  22.  
  23.            if (dec/2!=0 || dec==1)
  24.            {
  25.               while(dec!=1)
  26.               {
  27.                  max[i]=dec%2;
  28.                  dec = dec/2;
  29.                  i++;
  30.                }
  31.                  max[i]=1;
  32.             }
  33.                     System.out.print("The binary equivalent is "  );
  34.             for (int z=i; z>0; z--)
  35.             {
  36.                System.out.print(max[z]);
  37.             }
  38.  
  39.                    System.out.println("\n" + "Enter 1 for enter another value or 0 to exit:");
  40.                           String reply=scan.next();
  41.                     int b=Integer.parseInt(reply);
  42.                      if(b==1)
  43.                     a=1;
  44.                     else
  45.                     a=0;
  46. }
  47. }
  48. }
Sep 7 '08 #1
2 7020
Nepomuk
3,112 Expert 2GB
Actually, it doesn't work fine. For example, if I enter "10", it will give me "101" as the output. (Same result for "11" by the way.) Now, "101" is 1*2^0 + 0*2^1 + 1*2^2 = 1 + 0 + 4 = 5. The output you want is 1*8 + 0*4 + 1*2 + 0*1 = (1010)binary.

So, go through your code again and try to get it right.

Greetings,
Nepomuk
Sep 8 '08 #2
chaarmann
785 Expert 512MB
What do you want to achieve with:
Expand|Select|Wrap|Line Numbers
  1. if (dec/2!=0 || dec==1) 
  2. {
  3.  ... // convert
  4. }
???
You wrote: I am dividing dec by 2 (for whatever unknown reason) and if the result is not 0, then I convert. That is a complicated way of saying: I skip conversion if the user entered 0 or 1 or -1 as dec! Why did you write that? That doesn't make any sense to me. Then the if-expression goes on with: Or I convert when the user entered dec=1. But you want the opposite: when he entered 1 you want not to convert (but to restart or whatever).
That's quite contradictionary if the user entered "1": first expression says: skip conversion and second says: do it. And if you concatenate that with "or", you alway do it. And as a result this condition prevails: skip conversion if user entered -1 or 0, else do it. I assume that instead you want to skip conversion if the user has entered a 0 or 1, because in this case you don't want to convert, but exit or restart.
So why did you not simply write it in an intuitive and easy way as
Expand|Select|Wrap|Line Numbers
  1. if (dec!=0 && dec !=1) convert
?

Is this a program from your teacher that you should debug for learning purposes, or did you write it of your own? If you wrote if of your own, what was in your mind? How did you plan in which way it should work?

By answering these questions you will find the solution to your problem.
Sep 9 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: T K | last post by:
Anyone aware of such a converter? Anyone know why SUN is not all over it? tia -t
1
by: Ray Mitchell | last post by:
Hello, I have two Java applications that exchange serialized objects. Although I would like to ultimately convert both to C# it's a pretty monumental task, especially for someone just learning...
3
by: Brian Henry | last post by:
Does anyone know of or know how to convert a COBOL packed decimal in a text file to a decimal that .NET can work with? we are importing Old COBOL data files that have packed data in them and need...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
5
by: ria3 | last post by:
Hi, I have to write a program that will alow the user to specify a number (decimal, binary, or hexadecimal) and a base to convert to (decimal, binary, or hexadecimal) using subroutine methods and...
4
by: javamonster | last post by:
guys i need a help... i wanna write a java programmee to display the binary number of the entered decimal number. for ex : in t text area u should type a desimal number and press "Convert" button...
14
realin
by: realin | last post by:
hi guys i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011 now how do i swap it off.. ...
6
by: mearvk | last post by:
Does C++ or C have something roughly equivalent to this: http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html What I need is a way to quickly convert between decimal and binary and...
4
by: Yonih | last post by:
So I am trying to get this Calculator to work. It needs to take in a vaule, and select a shipping Everythin works great except the shipping part. I need it to take the shipping value and add it to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.