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));
}
}
S'il vous plaît minimiser le problème jusqu'à ce qu'il soit un [mcve] –
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
ouais le dernier chiffre de l'int je sais – Cam