Binary to Decimal and Octal Conversion

Discussion in 'Java' started by lei, Dec 30, 2006.

  1. lei

    lei Guest

    There are few errors in syntax. I'm new to java and I can't recognize
    why do such errors appear. Please help. Thank you so much!

    <code>
    import java.lang.*;
    import java.io.*;

    class BinaryDecoder{
    public static void main(String args[]) throws IOException{
    InputStreamReader stdin = new InputStreamReader(System.in);
    BufferedReader console = new BufferedReader(stdin);
    System.out.print("Enter a number in binary: ");
    String input = console.readLine();

    //Decimal value of input
    System.out.println("Decimal: " + toDecimal(input));

    //Converts to Octal
    System.out.print("Octal: ");
    for(int counter=input.length()-1; counter>=0; counter-=3){
    //groups the binary digits to 3
    String group=input.substring(counter,counter-2);
    //converts the grouped digit to binary
    System.out.print(toDecimal(group));
    }

    //Converts to Decimal
    public int toDecimal(String s){
    int sum=0;
    for(int counter=s.length()-1; counter>=0; counter--){
    if(s.charAt(counter)=='1'){
    int exp=s.length()-1-counter;
    sum+=Math.pow(2,exp);
    }
    }
    return sum;
    }
    }
    }

    </code>
     
    lei, Dec 30, 2006
    #1
    1. Advertising

  2. lei wrote:
    > There are few errors in syntax. I'm new to java and I can't recognize
    > why do such errors appear. Please help. Thank you so much!


    Your immediate problem is that you've put "toDecimal" inside "main". You
    can't do that.

    --
    John W. Kennedy
    "The blind rulers of Logres
    Nourished the land on a fallacy of rational virtue."
    -- Charles Williams. "Taliessin through Logres: Prelude"
     
    John W. Kennedy, Dec 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC
    Replies:
    2
    Views:
    4,732
    Brad BARCLAY
    Dec 30, 2003
  2. Raja
    Replies:
    2
    Views:
    14,993
  3. Mars
    Replies:
    16
    Views:
    646
    Chris Williams
    Feb 22, 2005
  4. bob
    Replies:
    6
    Views:
    761
    Peter Shaggy Haywood
    Mar 21, 2006
  5. Ven
    Replies:
    3
    Views:
    1,343
Loading...

Share This Page