Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Binary to Decimal Conversion

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary to Decimal Conversion

    Hey everyone, as I am totally new to Java programming, I am currently trying to convert a Binary number to Decimal. The task was to :

    Use a loop to read (charAt()) each digit (0/1 char) in the input string, scanning from right to left;
    Use the loop to build the required powers of 2;
    Use a conditional statement to deal with 0 and 1 separately;
    Debug using simple input, e.g. 1, 10, 101, and print intermediate values in the loop.

    Use your program to find the decimal value of the following binary number:
    1001010101011010111001011101010101010101


    public class BinarytoDecimal {
    public static void main (String[]args) {

    String digit = "1001010101011010111001011101010101010101";
    String output = "";
    for (int i= 0; i <digit.length(); i++) { // walk over all digits
    output*= 2; // prepare for another digit, see above
    output+= // digit at location i

    System.out.println(output);
    }
    }

    And with my very beginner knowledge of Java, I wrote the above code which still does not work. I would be delighted if anyone can help me with fixing the code?
    Last edited by Java_boy; January 6th, 2012 at 07:20 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Binary to Decimal Conversion

    I wrote the above code which still does not work.
    Please explain. Show what the code is doing and explain what it should have done.
    Try testing with a smaller String of binary digits before attempting to solve the long ones.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary to Decimal Conversion

    Well I gave it a go and when it is compiled the message given is "BUILD SUCCESSFUL (total time: 0 seconds)"
    The two errors are: (I have written in the syntax)

    public class BinarytoDecimal {
    public static void main (String[]args) {

    String digit = "1001010101011010111001011101010101010101";
    String output = "";
    for (int i= 0; i <digit.length(); i++) { // walk over all digits
    output*= 2; // error operator * cannot be applied to java.lang.String, int
    output+= // error operator + cannot be applied to java.lang.String, int

    System.out.println(output);
    }
    }

    Basically, I have two Strings, One which is String digit (which will be the binary number), other String output (which prepares an empty output initially). In order for me to convert my String digit into binary, I need to use a for loop which will build required powers of 2 and conditional statement which deals with 1 and 0 separately and using the loop to read (charAt()) each digit (0/1 char)( which the part I don't understand how to do) so i gave it a shot anyway.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Binary to Decimal Conversion

    output*= 2; // error operator * cannot be applied to java.lang.String, int
    The error message means what it says You can't multiply a string like output because string multiplication doesn't make sense. Consider making output an int so you can multiply it. You can always make a string from it later if you want to.

    output+= // error operator + cannot be applied to java.lang.String, int
    This line isn't finished. You have to add something to output. The chances are you will use an if statement and check what character you are looking at in the digit string. Depending on its value you will or will not add something to output.

  5. #5
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Binary to Decimal Conversion

    This is my opinion on this question: Do you know how to convert base ten numbers into binary format? If you answered yes to this question, then I think what you need to do is to reverse engineer the code you use in your conversion. If you answered No, then you might want to go back to the drawing board! Check out a tutorial or visit binary.com for tips on how to do such a conversion.

Similar Threads

  1. Decimal To binary Recursive conversion java?
    By sirdonclemo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:33 AM
  2. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM
  3. Decimal to binary conversion
    By baueml01 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2011, 06:42 AM
  4. [SOLVED] decimal to binary problem
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 26th, 2010, 07:48 PM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM