Foros del Web » Programación para mayores de 30 ;) » Java »

Conversion de Scanner

Estas en el tema de Conversion de Scanner en el foro de Java en Foros del Web. 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 ...
  #1 (permalink)  
Antiguo 17/02/2010, 17:43
 
Fecha de Ingreso: febrero-2010
Mensajes: 4
Antigüedad: 14 años, 2 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]));

}

}
}
  #2 (permalink)  
Antiguo 17/02/2010, 17:54
Avatar de fatherjuan  
Fecha de Ingreso: mayo-2005
Ubicación: Estado de México
Mensajes: 132
Antigüedad: 19 años
Puntos: 0
Respuesta: Conversion de Scanner

muestranos la traza del error, es más eficiente que revisar el código.
__________________
El principio de la sabiduría es el temor de Jehová
Visita mi Blog
  #3 (permalink)  
Antiguo 17/02/2010, 18:02
 
Fecha de Ingreso: febrero-2010
Mensajes: 4
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: Conversion de Scanner

Es que no da error, simplemente metas el numero que metas da:

Please, type a number:
8
Decimal Pure Binary 1's Complement 2's Complement
======= =========== =============== ===============
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000
0 0 00000000000000000000000000000000 00000000000000000000000000000000

Etiquetas: scanner
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:11.