Sunday, December 27, 2009

Binary to decimal java coding problem..help?

import java.io.*;


import java.lang.*;


import java.util.*;


class BinaryToDec{


public static void main(String[] args) throws IOException {


BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));


System.out.print(';Enter binary no:';);


String bin = bf.readLine();


long num = Long.parseLong(bin);


double rem;


while(num %26gt; 0){


rem = num % 10;


num = num / 10;


if(rem != 0 %26amp;%26amp; rem != 1){


System.out.println(';This is not a binary number.';);


System.out.println(';Please try once again.';);


System.exit(0);


}


}


double j=0;


double i=(Math.pow(2,j));


double bi=bi+(rem*i);


//int k=k+(rem*i)





//double s1=(s1+(a*(pow(2,j))));


j++;


//int i= Integer.parseInt(bin,2);


String deci = String.valueOf(i);


System.out.println(';decimal: '; + deci);


}


}





Please check the error and send me the correct code alsoBinary to decimal java coding problem..help?
I don't have java sdk installed so i did not get a chance to run the code but this is what i got by mere looking at your code.


import java.io.*;


import java.lang.*;


import java.util.*;


class BinaryToDec{


public static void main(String[] args) throws IOException {


BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));


System.out.print(';Enter binary no:';);


String bin = bf.readLine();


long num = Long.parseLong(bin);


double rem;


double j=0,i,bi=0;


while(num %26gt; 0){


rem = num % 10;


num = num / 10;


if(rem != 0 %26amp;%26amp; rem != 1){


System.out.println(';This is not a binary number.';);


System.out.println(';Please try once again.';);


System.exit(0);


}





//dont end while here.


i=(Math.pow(2,j));


bi=bi+(rem*i);


//int k=k+(rem*i)





//double s1=(s1+(a*(pow(2,j))));


j++;


}//end while here


//int i= Integer.parseInt(bin,2);


String deci = String.valueOf(bi);


//bi is the final decimal not i


System.out.println(';decimal: '; + deci);


}


}

No comments:

Post a Comment