Binary to Decimal and Octal Conversion

L

lei

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>
 
J

John W. Kennedy

lei said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top