17/02/2010, 17:43
|
| | Fecha de Ingreso: febrero-2010
Mensajes: 4
Antigüedad: 14 años, 9 meses Puntos: 0 | |
Conversion de Scanner Tengo un pequeño problemilla y es que no consigo hacer que este programa me funcione. Supuestamente me tendría que pasar de un numero decimal a binario puro, complemento a dos y complemento a uno, pero no me tira.
Pienso que el problema radica en el almacenamiento de la entrada por Scanner, pero no consigo dar con ello.
Os paso el codigo, por si me podeis hechar una mano.
Un Saludo y gracias por adelantado.
import java.io.IOException;
import java.util.Scanner;
/**
*
*
* This program calculates some binary representations of a given integer.
*/
public class Repr {
private static int[] numbers = new int[10];
public static void main(String[] args) throws IOException{
readNumber();
print();
}
/**
* Read an array of bytes and convert it to an integer. This integer is the one we will use to generate the numbers we need.
* @throws java.io.IOException
*/
public static void readNumber() throws java.io.IOException{
Scanner sc = new Scanner (System.in);
RepresentacionBinaria rb = new RepresentacionBinaria();
System.out.println("Please, type a number:");
int number = sc.nextInt();
}
/**
* This method return a String with the int converted to binary.
*/
public static String pureBinary(int number){
String pureBin = "0";
StringBuffer buf= new StringBuffer();
if(number == 0){
return pureBin;
}
int remaining = Math.abs(number);
while(remaining > 0){
if(remaining%2 == 0){
buf.append("0");
}else{
buf.append("1");
}
remaining = remaining / 2;
}
return pureBin = buf.reverse().toString();
}
/**
* This method return a String with the int converted to 1s complement binary.
* @param number The number we want to convert.
* @return comp1 String with the number in 1s complement binary.
*/
public static String complement1(int number){
String comp1 = "";
StringBuffer buf = new StringBuffer("0000000000000000000000000000000");
comp1 = pureBinary(number);
buf.replace(buf.length() - comp1.length() +1, buf.length(), comp1);
if(number >= 0)
return comp1 = buf.toString();
for(int i = 0; i < buf.length(); i++){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
}else{
buf.replace(i, i+1, "0");
}
}
return comp1 = buf.toString();
}
/**
* This method return a String with the int converted to 2s complement binary.
* @param number The number we want to convert.
* @return comp2 String with the number in 2s complement binary.
*/
public static String complement2(int number){
String comp2 = "";
StringBuffer buf = new StringBuffer(complement1(number));
if (number >= 0)
return comp2 = buf.toString();
for(int i = buf.length() -1; i > 0; i--){
if(buf.charAt(i) == '0'){
buf.replace(i, i+1, "1");
return comp2 = buf.toString();
}else{
buf.replace(i, i+1, "0");
}
}
return comp2 = buf.toString();
}
/**
* Prints the results
*/
public static void print(){
System.out.println("Decimal\t\tPure Binary\t\t1's Complement\t\t\t\t2's Complement");
System.out.println("=======\t\t===========\t\t==== ===========\t\t\t\t===============");
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]+"\t\t"+pureBinary(numbers[i])+"\t\t"+
complement1(numbers[i])+"\t"+complement2(numbers[i]));
}
}
} |