首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用用户输入将十六进制(0x)或二进制(0b)转换为十进制数字

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

Stack Overflow用户
提问于 2015-02-17 07:03:45
回答 1查看 3.8K关注 0票数 0

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

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

我不知道如何使这个计划奏效。帮帮忙我在这里少了什么?

代码语言:javascript
运行
复制
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");
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-17 07:13:09

因为您试图转换整个字符串,包括0x或0b,所以会出现错误。在转换之前,您需要从字符串中删除它。以下是完整的工作示例:

代码语言:javascript
运行
复制
 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();

代码语言:javascript
运行
复制
    //read input as string
    String hexString = input.nextLine();
    String binString = input.nextLine();

但是可以使用单个字符串输入,如: //read输入作为字符串consoleInput = input.nextLine();

代码语言:javascript
运行
复制
        //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");
        }

如果由于某种原因,用户输入无法转换,我很高兴添加“尝试捕捉”。下面是一个有用的例子:

代码语言:javascript
运行
复制
 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");
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28556426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档