-1

我需要使用不同的方法将十六进制转换为十进制。当我输入正确的十六进制数字时,我的程序会显示十进制值并表示该数字有效。但是,当我输入不正确的十六进制值时,我的程序会崩溃。这是我的代码:

import java.io.*;

import java.util.Scanner;

public class pg3a {

 public static void main(String[] args)  throws IOException {

   Scanner keyboard = new Scanner(System.in);

     String hex;
     char choice = 'y';
     boolean isValid = false;
  do {
     System.out.print("Do you want to enter a hexadecimal number? ");
     System.out.print("y or n?: ");
     choice = keyboard.next().charAt(0);
 switch(choice){
    case 'y':
      System.out.print("Enter a hexadecimal number: #");
      hex = keyboard.next();
      hex = hex.toUpperCase();
      int hexLength = hex.length();
      isValid = valid(hex);
      Integer value = Integer.parseInt(hex,16);
      System.out.println("The value: " + value);

   if (isValid) {
      System.out.println(hex + " is valid");
  }
   break;
  case 'n':
      System.out.print("Quit");
  }
  }while (choice != 'n');
  }

public static boolean valid (String validString) {

  int a = 0;
  if (validString.charAt(0) == '-') {
  a = 1;
  }
    for (int i=a; i< validString.length(); i++) {
       if (!((validString.charAt(i) >= 'A' && validString.charAt(i) <= 'F')|| (validString.charAt(i) >= 0 && validString.charAt(i) <= 9)))
   {
   return false;
   }
   }
   return true;
   }


  public static long convert (String hexValue) {

    long decimal = 0;
    boolean isNegative = false;
    int a = 0;

    if (hexValue.charAt(0) == '-') {
    isNegative = true;
    a = 1;
   }
   for (int i = a; i<hexValue.length(); i++) {
      decimal = decimal*16;
        if (hexValue.charAt(i) >= '0' && hexValue.charAt(i) <= '9') {
        decimal += hexValue.charAt(i) - '0';
     }
       else if (hexValue.charAt(i) >= 'a' && hexValue.charAt(i) <= 'f') {
          decimal += hexValue.charAt(i) - 'a' + 10;
     }
     }
       if (isNegative == true) {
         decimal *= -1;
    }
    return decimal;
    }
    }

为什么它会崩溃,我该如何修复它,以便在输入不正确的十六进制数字时显示“无效”?

4

3 回答 3

0

如果输入无效的十六进制数,Integer.parseInt()将抛出NumberFormatException. 像这样更改您的代码:

...
isValid = valid(hex);

if (isValid) {
    Integer value = Integer.parseInt(hex,16);
    System.out.println("The value: " + value);
    System.out.println(hex + " is valid");
}
...
于 2013-10-02T06:32:39.853 回答
0

在 if 语句中添加Integer value = Integer.parseInt(hex,16);并在 else 块中打印无效。

 if (isValid) {
       Integer value = Integer.parseInt(hex,16);
      System.out.println("The value: " + value);
      System.out.println(hex + " is valid");
  }
  else{
   System.out.println("invalid");
  }

更新:

更改您的有效方法如下:

    public static boolean valid(String validString) {

        int a = 0;
        if (validString.charAt(0) == '-') {
            a = 1;
        }
        for (int i = a; i < validString.length(); i++) {
//            if (!((validString.charAt(i) >= 'A' && validString.charAt(i) <= 'F') || (validString.charAt(i) >= 0 && validString.charAt(i) <= 9))) {
//                return false;
//            }
            char ch=validString.charAt(i);
            if(!(Character.isDigit(ch) || (Character.isLetter(ch) && ((ch-'A')<=5))) ){
            return false;
           }
        }
        return true;
    }
于 2013-10-02T06:35:20.173 回答
0
do {
  System.out.print("Do you want to enter a hexadecimal number? ");
  System.out.print("y or n?: ");
  choice = keyboard.next().charAt(0);
  int base = 10;
  switch(choice)
  {
    case 'y':
      System.out.print("Enter a hexadecimal number: #");
      hex = keyboard.next();
      hex = hex.toUpperCase(); //I'm not sure if this step is necessary
      try {
          Integer value = Integer.parseInt(hex, 16);
          System.out.println("Valid hex format");
          System.out.println("Hex: " + hex);
          System.out.println("Decimal: " + value);

      }
      catch (NumberFormatException e) {
          System.out.println("Invalid hex format");
          System.out.println("Input: " + hex);
      }
      break;
    case 'n':
      System.out.print("Quit");
      break
  }
} while (choice != 'n');

现在您可以删除所有辅助方法

于 2013-10-02T06:38:33.353 回答