使用用户输入将十六进制(0x)或二进制(0b)转换为十进制数

时间:2015-02-17 07:03:45

标签: java binary hex decimal

用户输入十六进制或二进制数字(必须以0x或0b为前缀,如果没有则给出错误信息)。 将用户输入读为字符串

我不能使用基数,我只使用integer.parseInt(s)

我不确定如何使这个程序工作..请帮忙 我在这里失踪了什么?

import java.util.Scanner;
public class conversion {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    //ask user to enter either hex or binary
    System.out.println("Please enter a hex (0x) or binary (0b) number: ");

    //read input as string
    String hexString = input.nextLine();
    String binString = input.nextLine();


    //if prefix (0x)
    if ((hexString.substring(0,2)).equals("0x")) {
        int hex = Integer.parseInt(hexString, 16);
        System.out.println("Converting from base-16 to base-10.\n" + hex);

        //if there are no digits or invalid digits after Ox
        //if ((hexString.substring(beginIndex))) {
        //  System.out.println("Error parsing base-16 number");
        //}

    //if prefix (0b)    
    } else if ((binString.substring(0,2)).equals("0b")) {
        int bin = Integer.parseInt(binString, 2);
        System.out.println("Converting from base-2 to base-10.\n" + bin);

        //if there are no digits or invalid digits after Ob
        //if ((binString.substring(beginIndex))) {
        //  System.out.println("Error parsing base-2 number");
        //}


    } else {
        System.out.println("I don't know how to covert that number");
    }

1 个答案:

答案 0 :(得分:0)

您收到错误是因为您尝试转换整个字符串,包括0x或0b。您需要在转换之前将其从字符串中删除。以下是完整的工作示例:

 public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //ask user to enter either hex or binary
        System.out.println("Please enter a hex (0x) or binary (0b) number: ");

        //read input as string
        String hexString = input.nextLine();
        String binString = input.nextLine();

        //if prefix (0x)
        if (hexString.startsWith("0x")) {
            hexString = hexString.replaceAll("0x", "");
            int hex = Integer.parseInt(hexString, 16);
            System.out.println("Converting from base-16 to base-10.\n" + hex);

            //if there are no digits or invalid digits after Ox
            //if ((hexString.substring(beginIndex))) {
            //  System.out.println("Error parsing base-16 number");
            //}
            //if prefix (0b)    
        } else if (binString.startsWith("0b")) {
            binString = binString.replaceAll("0x", "");
            int bin = Integer.parseInt(binString, 2);
            System.out.println("Converting from base-2 to base-10.\n" + bin);

            //if there are no digits or invalid digits after Ob
            //if ((binString.substring(beginIndex))) {
            //  System.out.println("Error parsing base-2 number");
            //}
        } else {
            System.out.println("I don't know how to covert that number");
        }
    }

所有要优化您的代码,您不需要像在此处一样使用input.nextLine();两次:

    //read input as string
    String hexString = input.nextLine();
    String binString = input.nextLine();

但你可以使用单字符串输入,如:             //将输入读为字符串             String consoleInput = input.nextLine();

        //if prefix (0x)
        if (consoleInput.startsWith("0x")) {
            consoleInput = consoleInput.replaceAll("0x", "");
            // ...
        } else if (consoleInput.startsWith("0b")) {
            consoleInput = consoleInput.replaceAll("0x", "");
            int bin = Integer.parseInt(consoleInput, 2);
            // ...
        } else {
            System.out.println("I don't know how to covert that number");
        }

如果由于某种原因无法转换用户输入,我很高兴添加try catch。这是一个有效的例子:

 public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //ask user to enter either hex or binary
        System.out.println("Please enter a hex (0x) or binary (0b) number: ");

        //read input as string
        String consoleInput = input.nextLine();

        //if prefix (0x)
        if (consoleInput.startsWith("0x")) {
            consoleInput = consoleInput.replaceAll("0x", "");

            try {
                int hex = Integer.parseInt(consoleInput, 16);
                System.out.println("Converting from base-16 to base-10.\n" + hex);
            } catch (NumberFormatException ex) {
                System.out.println("Error converting string: " + consoleInput);
            }

        } else if (consoleInput.startsWith("0b")) {
            consoleInput = consoleInput.replaceAll("0x", "");

            try {
                int bin = Integer.parseInt(consoleInput, 2);
                System.out.println("Converting from base-2 to base-10.\n" + bin);
            } catch (NumberFormatException ex) {
                System.out.println("Error converting string: " + consoleInput);
            }

        } else {
            System.out.println("I don't know how to covert that number");
        }
    }