Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Binary to Decimal and Octal Conversion (http://www.velocityreviews.com/forums/t389744-binary-to-decimal-and-octal-conversion.html)

 lei 12-30-2006 03:22 AM

Binary to Decimal and Octal Conversion

There are few errors in syntax. I'm new to java and I can't recognize

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

class BinaryDecoder{
public static void main(String args[]) throws IOException{
System.out.print("Enter a number in binary: ");

//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 12-30-2006 04:01 AM

Re: Binary to Decimal and Octal Conversion

lei wrote:
> There are few errors in syntax. I'm new to java and I can't recognize

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"

 All times are GMT. The time now is 05:15 AM.