【问题标题】:using user input to convert hex (0x) or binary (0b) to decimal numbers使用用户输入将十六进制 (0x) 或二进制 (0b) 转换为十进制数
【发布时间】:2015-04-17 20:23:00
【问题描述】:

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

【问题讨论】:

    标签: java binary hex decimal


    【解决方案1】:

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

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

    【讨论】:

    • 哦,谢谢! @Kiki 如果用户输入前缀 0x 或 0b 后面没有数字,或者有无效数字,我该如何添加概念,以便我可以告诉他们无法转换?
    • 在转换之前添加这个 if (consoleInput.trim().isEmpty()) { System.out.println("Can't convert");返回;如果它有帮助,请不要忘记接受答案:)
    • 很抱歉用问题轰炸你..刚刚迷路了,所以如果我想添加输入没有有效前缀的部分......或者如果它有一个有效的前缀,但是前缀后没有数字(只有 0x 或 0b,后面没有任何数字),结束程序并说我不知道​​如何转换该数字以及输入是否包含指定基数的无效数字(例如 F,或15,对于0b或base-2),以错误转换消息结束程序,我必须有更复杂的代码还是有简单的方法来做到这一点? @琪琪
    猜你喜欢
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2014-10-30
    • 2016-08-28
    相关资源
    最近更新 更多