Error de conversion
Tengo un problema. Supuestamente este programa pasa de un numero decimal a binario puro, complemento a 1 y complemento a dos, pero no se por que narices no funciona. Si me pudierais hechar una mano os estaria muy agradecido.
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("DecimalttPure Binarytt1's Complementtttt2's Complement");
System.out.println("=======tt===========tt===============tttt===============");
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]+"tt"+pureBinary(numbers[i])+"tt"+
complement1(numbers[i])+"t"+complement2(numbers[i]));
}
}
}
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("DecimalttPure Binarytt1's Complementtttt2's Complement");
System.out.println("=======tt===========tt===============tttt===============");
for(int i = 0; i < numbers.length; i++){
System.out.println(numbers[i]+"tt"+pureBinary(numbers[i])+"tt"+
complement1(numbers[i])+"t"+complement2(numbers[i]));
}
}
}
De echo creo que el problema radica en la utilización del Scanner, pero no se en que punto falla.
Hola!
Aver a simple vista...
en:
public static void main(String[] args) throws IOException{
readNumber();
print();
}
falta llamar a los metodos....para que haga las operaciones.
le echo un vistazo probandolo yo..y luego te comento mas...
Aver a simple vista...
en:
public static void main(String[] args) throws IOException{
readNumber();
print();
}
falta llamar a los metodos....para que haga las operaciones.
le echo un vistazo probandolo yo..y luego te comento mas...
Por cierto, se me olvidaba decir que se me habia olvidado cambiar el nombre de la clase para que coincida con el array del Scanner.
Funciona, pero ahora el problema es que no me pide un numero y me saca este y los 10 siguientes, si no que me pide que 10 numeros quiero sacar, uno por uno.
a vale querias que te pidiera un numero y sake los siguientes...!!
esqeu no sabia lo que querias...
muy facil..mira aver asi:
public static void readNumber( int[] numbers) throws java.io.IOException{
Scanner sc = new Scanner (System.in);
int number=0;
System.out.println("Please, type a number:");
number = sc.nextInt();
for (int c=0;c<numbers.length;c++){
numbers[c]=number+c;
}
}
esqeu no sabia lo que querias...
muy facil..mira aver asi:
public static void readNumber( int[] numbers) throws java.io.IOException{
Scanner sc = new Scanner (System.in);
int number=0;
System.out.println("Please, type a number:");
number = sc.nextInt();
for (int c=0;c<numbers.length;c++){
numbers[c]=number+c;
}
}
Buah!!! Increible. Despues de rebanarme la cabeza con esto era asi de simple.
De verdad, muchas gracias por la ayuda, ahora por fin me puedo ir a dormir. Te debo varias horas de sueño Kalua66!!!
De verdad, muchas gracias por la ayuda, ahora por fin me puedo ir a dormir. Te debo varias horas de sueño Kalua66!!!
Denada pa eso estamos
...aveces las cosas mas simples se nos pasan...
y tiene que verlas otra persona...
...aveces las cosas mas simples se nos pasan...
y tiene que verlas otra persona...
Vale ya se donde falla....
Mandas leer un numero que no guardas en ningun lao...
lo mas normal seria acer un bucle en el metodo de leer para guardar 10 numeros en el array que tienes.
y luego ya esta...asique al metodo de leer le tienes que pasar el array...
asique modifica el metodo read...
yo lo solucione asi:
public static void readNumber( int[] numbers) throws java.io.IOException{
Scanner sc = new Scanner (System.in);
int number=0;
for (int c=0;c<numbers.length;c++){
System.out.println("Please, type a number:");
number = sc.nextInt();
numbers[c]=number;
}
}
Mandas leer un numero que no guardas en ningun lao...
lo mas normal seria acer un bucle en el metodo de leer para guardar 10 numeros en el array que tienes.
y luego ya esta...asique al metodo de leer le tienes que pasar el array...
asique modifica el metodo read...
yo lo solucione asi:
public static void readNumber( int[] numbers) throws java.io.IOException{
Scanner sc = new Scanner (System.in);
int number=0;
for (int c=0;c<numbers.length;c++){
System.out.println("Please, type a number:");
number = sc.nextInt();
numbers[c]=number;
}
}