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

using user input to convert hex (0x) or binary (0b) to decimal numbers

用户输入一个十六进制或二进制的数字(必须以 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");
    }

您收到错误消息是因为您正在尝试转换包括 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();

但是您可以使用单个字符串输入,例如: //将输入读取为字符串 字符串控制台输入 = 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");
        }
    }