2016-05-03 1 views
1

J'essaye d'écrire un programme pour convertir des nombres de n'importe quel système numérique en n'importe quel système numérique (binaire/décimal/octal/hex). L'ensemble du programme fonctionne sauf pour convertir les nombres hexadécimaux en nombres binaires/décimaux/octaux. Voici mon code:NumberFormatException lors de la conversion de nombres hexadécimaux

import javax.swing.JOptionPane; 

public class convertNumber { 
public static void main(String[] args){ 

    //Getting the numeral system input from the user 
    String numsystem1 = JOptionPane.showInputDialog("Please enter the numeral system that you want to convert FROM: binary, octal, decimal or hexadecimal."); 

    //Validating if the system is written correctly 
    if (numsystem1.equalsIgnoreCase("Binary") || numsystem1.equalsIgnoreCase("Octal") || numsystem1.equalsIgnoreCase("Decimal") || numsystem1.equalsIgnoreCase("Hexadecimal")) 
    {System.out.println ("You are converting from " + numsystem1 + " numeral system:"); 

    //Getting the number input from the user 
    int number = Integer.parseInt (JOptionPane.showInputDialog("Please enter the number that you want to convert.")); 

    //Binary number conversion 
    if (numsystem1.equalsIgnoreCase ("Binary")) { 
     String bin1 = String.valueOf (number); 
     //To decimal 
     int dec1 = Integer.parseInt(bin1,2); 
     //To hexadecimal 
     String hex1 = Integer.toHexString (dec1); 
     //To octal 
     String oct1 = Integer.toOctalString(dec1); 
     System.out.println ("Binary:  " + bin1); 
     System.out.println ("Octal:  " + oct1); 
     System.out.println ("Decimal:  " + dec1); 
     System.out.println ("Hexadecimal: " + hex1); 
    } 

    //Octal number conversion 
    else if (numsystem1.equalsIgnoreCase ("Octal")) { 
     String oct1 = String.valueOf (number); 
     //To decimal 
     int dec1 = Integer.parseInt(oct1,8); 
     //To hexadecimal 
     String hex1 = Integer.toHexString (dec1); 
     //To binary 
     String bin1 = Integer.toBinaryString(dec1); 
     System.out.println ("Octal:  " + oct1); 
     System.out.println ("Binary:  " + bin1); 
     System.out.println ("Decimal:  " + dec1); 
     System.out.println ("Hexadecimal: " + hex1); 
    } 

    //Decimal number conversion 
    else if (numsystem1.equalsIgnoreCase ("Decimal")) { 
     String dec2 = String.valueOf (number); 
     int dec1 = Integer.parseInt(dec2,10); 
     //To binary 
     String bin1 = Integer.toBinaryString(dec1); 
     //To octal 
     String oct1 = Integer.toOctalString (dec1); 
     //To hexadecimal 
     String hex1 = Integer.toHexString (dec1); 
     System.out.println ("Decimal:  " + dec1); 
     System.out.println ("Binary:  " + bin1); 
     System.out.println ("Octal:  " + oct1); 
     System.out.println ("Hexadecimal: " + hex1); 
    } 

    //Hexadecimal number conversion 
    else if (numsystem1.equalsIgnoreCase ("Hexadecimal")) { 
     String hex1 = Integer.toHexString (number); 
     //To decimal 
     int dec1 = Integer.parseInt(hex1); 
     //To binary 
     String bin1 = Integer.toBinaryString(number); 
     //To octal 
     String oct1 = Integer.toOctalString (number); 
     System.out.println ("Hexadecimal: " + hex1); 
     System.out.println ("Binary:  " + bin1); 
     System.out.println ("Octal:  " + oct1); 
     System.out.println ("Decimal:  " + dec1); 
     } 

    else 
    System.out.println ("Please enter the valid system."); 
}}} 

Qu'est-ce que je fais mal?

+0

double possible de [Qu'est-ce qu'un NumberFormatException et comment puis-je corriger ?] (http://stackoverflow.com/questions/39849984/what-is-a-num berformatexception-et-comment-peut-je-fixer-it) – xenteros

Répondre

2

Depuis hex1 est en hexadécimal, ce

int dec1 = Integer.parseInt(hex1); 

devrait être

int dec1 = Integer.parseInt(hex1, 16); 

ou

String hex1 = Integer.toHexString (number); 
int dec1 = number; // <-- not clear why you need to parse the hex