ex: 1001010100100010
I only have to take 10010101 first 8 of them. Can someone help me with this . I need a function that can do this stuff.
this is my program :
import java.io.*;
public class BynaryFloat
{
/** Creates a new instance of BynaryFloat */
public BynaryFloat()
{
}
public static void main(String[] args)throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("What do you want the program to do?");
System.out.println(" Decimal to binary ... 1");
System.out.println(" Binary to decimal ... 2");
String choice = input.readLine();
float c = Integer.parseInt(choice);
if(c==1)
{
System.out.println(" Enter a decimal number:");
String dba=input.readLine();
int db = Integer.parseInt(dba);
System.out.println(db);
int [] m = new int [10];
int j, A;
for(j=8;j>=1;j--)
{
m[j] = db%2;
db = db/2;
}
int sc = 0;
for(j=1;j<=8;j++)
{
if(m[j]==0)
{
if(sc==0)
{
continue;
}
}
System.out.print(+m[j] + " ");
sc = sc+1;
}
}
if(c==2)
{
System.out.println("Enter a binary number");
String bda=input.readLine();
float l=bda.length()-1;
double sum =0;
for(int w =0;w<bda.length();w++)
{
char r = bda.charAt(w);
String str = new Character®.toString();
float er = Integer.parseInt(str);
sum = sum + (er*Math.pow(2,l));
l--;
}
System.out.println("The decimal is : "+sum);
}
}
}

New Topic/Question
Reply




MultiQuote





|