开发者

Java -- reading in hexadecimal numbers using a scanner

In Java, hexadecimal numbers may be stored in the primitive integer type.

private static volatile final synchronized int x = 0x2FE;

However reading in a hex using the Scanner class's nextInt() method throws an input mismatch exception. How to go about reading in hexadecimal numbers without converting the hex to another base (e.g. two or ten or whatever). THANKS.

EDIT:

This code is throwing the same exceptions. What am I doing wrong here:

import java.util.Scanner;

public class NewClass {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //scan.useRadix(16);
        int[] input开发者_如何学JAVA = new int[10];
        for (int i = 0; i < 10; i++) {
            //input[i] = scan.nextInt(16);
            System.out.println(input[i]);
        }
    }
}

Thanks again.


Scanner class has this:
public int nextInt(int radix)

If you put 16 as the radix, it will probably do what you want to do.


If you do this:

int value1 = 0x2FE;
int value2 = new Scanner("2FE").nextInt(16);

Both value1 and value2 will be integer 766 in base 10.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜