2017-09-07 4 views
1

Pour les devoirs, j'ai dû faire un convertisseur binaire en décimal sans l'utilisation de méthodes Java intégrées qui effectuent automatiquement cette conversion. Quand j'essaye d'obtenir le dernier nombre de l'entier qui est entré il me donne une exception de format de nombre.Comment obtenir le dernier numéro d'une entrée entière

import java.util.Scanner; 

public class Homework02 { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 

     System.out.println("Enter an 8-bit binary number:"); 
     int binary = keyboard.nextInt(); 
     int copyBinary = binary; 
     int firstDigit = Integer.parseInt(Integer.toString(copyBinary).substring(0, 1)); 
     int secondDigit = Integer.parseInt(Integer.toString(copyBinary).substring(1, 2)); 
     int thirdDigit = Integer.parseInt(Integer.toString(copyBinary).substring(2, 3)); 
     int fourthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(3, 4)); 
     int fifthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(4, 5)); 
     int sixthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(5, 6)); 
     int seventhDigit = Integer.parseInt(Integer.toString(copyBinary).substring(6, 7)); 
     int eigthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(7)); 

     firstDigit = firstDigit*128; 
     secondDigit = secondDigit*64; 
     thirdDigit = thirdDigit*32; 
     fourthDigit = fourthDigit*16; 
     fifthDigit = fifthDigit*8; 
     sixthDigit = sixthDigit*4; 
     seventhDigit = seventhDigit*2; 
     eigthDigit = eigthDigit*1; 

     System.out.println(firstDigit+" "+secondDigit+" " +thirdDigit+" "+fourthDigit+" "+fifthDigit+" "+sixthDigit+" "+seventhDigit+" "+eigthDigit); 

     System.out.println(copyBinary + " in decimal form is " + (firstDigit+secondDigit+thirdDigit+fourthDigit+fifthDigit+sixthDigit+seventhDigit+eigthDigit)); 
    } 

} 
+0

S'il vous plaît minimiser le problème jusqu'à ce qu'il soit un [mcve] –

+0

Par « dernier numéro d'une entrée entière » voulez-vous dire chiffres? Et si vous entrez juste moins ou plus de 8 bits? – lurker

+0

ouais le dernier chiffre de l'int je sais – Cam

Répondre

1

zéros en tête sont ignorés lorsque vous analyser et de formater un int. La solution la plus simple est de garder la pleine valeur en tant que chaîne et seulement ensuite analyser les chiffres individuels:

String binary = keyboard.next(); 
int firstDigit = Integer.parseInt(binary.substring(0, 1)); 
// etc. 
+0

Merci pour l'ami d'assistance! – Cam

0

Ce que je propose dans les commentaires est de lire toute entrée comme chaîne puis convertir un caractère à la temps en entier

Scanner keyboard = new Scanner(System.in); 
System.out.println("Enter an 8-bit binary number:"); 
String input = keyboard.nextLine(); 
// need to validate input here 
int dec = 0; 
for (int i=0; i<input.length(); i++) { 
    int x = Character.getNumericValue(input.charAt(input.length()-1-i)); 
    dec += x * Math.pow(2, i); 
} 
System.out.println("For binary number " + input + " its decimal value is " + dec);