Tuesday, July 28, 2009

Java code for binary to decimal?

hello all





i m trying for java code to convert binary to decimal


I hv written this code but it is not working





import java.util.*;





public class BinaryToDec


{


public static void main(String args[])


{


int sum = 0;





for (int i=0;i%26lt;args[0].length;i++)


{


char c = args[0].charAt(i)


int a;


a = Integer.parseInt(c);


if (a==0)


sum=sum+a;


elseif (a==1)


sum = sum + Math.pow(2,i);


}





System.out.println("Decimal = "+sum);


}


}








PLS HELP ME TO POINT OUT THE ERROR

Java code for binary to decimal?
Check this out http://saloon.javaranch.com/cgi-bin/ubb/...
Reply:You have lots of syntax errors





make the following corrections





for (int i=0;i%26lt;args[0].length;i++) --- %26gt;


for (int i=0;i%26lt;args[0].length();i++)





char c = args[0].charAt(i) ------%26gt;


String c = args[0].substring(i,i);








elseif (a==1) --------%26gt;


else if (a==1)








sum = sum + Math.pow(2,i); -------%26gt;


sum = sum + (int) Math.pow(2,i);








then give it another test....from what I can see, your logic is fine.


No comments:

Post a Comment