Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Binary to Decimal and Octal Conversion

Reply
Thread Tools

Binary to Decimal and Octal Conversion

 
 
lei
Guest
Posts: n/a
 
      12-30-2006
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>

 
Reply With Quote
 
 
 
 
John W. Kennedy
Guest
Posts: n/a
 
      12-30-2006
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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
newbie: hex, decimal, binary conversion bob C Programming 6 03-21-2006 01:51 AM
the algorithm of decimal<->binary conversion Mars C Programming 16 02-22-2005 01:51 AM
Inbuilt decimal to binary conversion function in C++ Raja C++ 2 06-17-2004 11:10 AM
Binary-Coded Decimal conversion kelvSYC Java 2 12-30-2003 04:06 PM



Advertisments