1
数値を任意の数値体系(2進/ 10進/ 8進/ 16進)に変換するプログラムを作成しようとしています。プログラム全体は、16進数を2進/ 10進/ 8進数に変換する以外は機能します。ここに私のコード:16進数を変換するときにNumberFormatExceptionが発生する
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.");
}}}
私は間違っている? hex1
以来
可能な重複する必要があります?](http://stackoverflow.com/questions/39849984/what-is-a-num私はそれを知っていますか? – xenteros