Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Binary to Decimal and Octal Conversion

Thread view: 
lei - 30 Dec 2006 03:22 GMT
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>
John W. Kennedy - 30 Dec 2006 04:01 GMT
> 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.

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.