need help with my binary numbersbinary numbers in java
Page 1 of 1
6 Replies - 1438 Views - Last Post: 29 September 2007 - 10:39 AM
#1
need help with my binary numbers
Posted 28 September 2007 - 09:29 AM
I AM TRYING TO CREAT MY PROGRAM, BUT IT GIVES ME A LOT OF ERRORS THAT I CAN FIX IT. PLEASE SOMEONE CAN HELP ME TO CREAT THIS PROGRAM. THANKS GUYS.
EX: BINARY->DECIMAL
INPUT: 111111001
OUTPUT: -7
EX: DECIMAL->BINARY
INPUT:-7
OUTPUT:111111001
MUCH DETAILS:
HERES HOW IT IS WORKING: BINARY->DECIMAL
YOU PUT 1001 AND IT FLIPS TO 0110. THEN YOU USE ALGORITME FOR 0110 AND IT GIVES YOU 6. THEN YOU ADD ONE AND IT GIVES YOU 7. AFTER THAT YOU INPUT NEGATIVE IN FRONT, AND YOUR FINAL RESULT BECOMES -7.
DECIMAL->BINARY:
YOU PUT 7 AND THEN ADD -1 GIVES YOU 6. THEN YOU USE ALGORITM YOU GET 6->0110. AFTER THAT FLIT THE NUMBER 0110->1001 AND IT WILL BE YOU ANSWER.
Replies To: need help with my binary numbers
#2
Re: need help with my binary numbers
Posted 28 September 2007 - 10:39 AM
Also, typing in all caps is annoying. It's the equivalent of yelling. Best to avoid it.
This post has been edited by Programmist: 28 September 2007 - 10:41 AM
#3
Re: need help with my binary numbers
Posted 28 September 2007 - 10:46 AM
Programmist, on 28 Sep, 2007 - 10:39 AM, said:
Also, typing in all caps is annoying. It's the equivalent of yelling. Best to avoid it.
my problem is that I can't replace i with 0 or with 1. Its says that int to int is not posible to replace.
package Bynary;
import java.util.*;
import java.io.*;
public class Program
{
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();
int 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;
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;
}
input.readLine();
}
if(c==2)
{
System.out.println("Enter a binary number");
String bda=input.readLine();
int l=bda.length()-1;
double sum =0;
for (int i = bda.length() - 1; i >= 0; i--)
{
int j = bda.indexOf(i);
if (i <= 0)
{
bda = bda.replace(i, 0);
j = bda.indexOf(i);
//bda = bda + "1";
}
else if (i >= 1)
{
bda = bda.replace(i, 1);
//bda = bda + "1";
}
}
for(int w =0;w<bda.length();w++)
{
char r = bda.charAt(w);
String str = new Character®.toString();
int er = Integer.parseInt(str);
sum = sum + (er*Math.pow(2,l));
l--;
}
System.out.println("The decimal is : "+sum);
System.out.println("-" + sum);
input.readLine();
}
}
}
[edit] added code tags
This post has been edited by PennyBoki: 28 September 2007 - 11:28 AM
#4
Re: need help with my binary numbers
Posted 28 September 2007 - 11:15 AM
Also, when you post code, put it in code tags. Another thing. It's usually best to copy and paste your exact error message. Thanks.
#5
Re: need help with my binary numbers
Posted 28 September 2007 - 11:51 AM
I AM TRYING TO CREAT MY PROGRAM, BUT IT GIVES ME A LOT OF ERRORS THAT I CAN FIX IT. PLEASE SOMEONE CAN HELP ME TO CREAT THIS PROGRAM. THANKS GUYS.
EX: BINARY->DECIMAL
INPUT: 111111001
OUTPUT: -7
EX: DECIMAL->BINARY
INPUT:-7
OUTPUT:111111001
MUCH DETAILS:
HERES HOW IT IS WORKING: BINARY->DECIMAL
YOU PUT 1001 AND IT FLIPS TO 0110. THEN YOU USE ALGORITME FOR 0110 AND IT GIVES YOU 6. THEN YOU ADD ONE AND IT GIVES YOU 7. AFTER THAT YOU INPUT NEGATIVE IN FRONT, AND YOUR FINAL RESULT BECOMES -7.
DECIMAL->BINARY:
YOU PUT 7 AND THEN ADD -1 GIVES YOU 6. THEN YOU USE ALGORITM YOU GET 6->0110. AFTER THAT FLIT THE NUMBER 0110->1001 AND IT WILL BE YOU ANSWER.
my problem is that I can't replace i with 0 or with 1. Its says that int to int is not posible to replace.
package Bynary;
import java.util.*;
import java.io.*;
public class Program
{
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();
int 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;
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;
}
input.readLine();
}
if(c==2)
{
System.out.println("Enter a binary number");
String bda=input.readLine();
int l=bda.length()-1;
double sum =0;
for (int i = bda.length() - 1; i >= 0; i--)
{
int j = bda.indexOf(i);
if (i <= 0)
{
bda = bda.replace(i, 0);
j = bda.indexOf(i);
//bda = bda + "1";
}
else if (i >= 1)
{
bda = bda.replace(i, 1);
//bda = bda + "1";
}
}
for(int w =0;w<bda.length();w++)
{
char r = bda.charAt(w);
String str = new Character®.toString();
int er = Integer.parseInt(str);
sum = sum + (er*Math.pow(2,l));
l--;
}
System.out.println("The decimal is : "+sum);
System.out.println("-" + sum);
input.readLine();
}
}
}
This post has been edited by PennyBoki: 28 September 2007 - 12:48 PM
#6
Re: need help with my binary numbers
Posted 28 September 2007 - 12:48 PM
#7
Re: need help with my binary numbers
Posted 29 September 2007 - 10:39 AM
|
|

New Topic/Question
Reply




MultiQuote





|