从文本文件扫描扫描仪吃数字的数字?

时间:2016-03-31 18:19:47

标签: java

我创建了一个名为“binary”的文本文件。其中包含数字0-15的二进制值(二进制值的格式为四位数字,如-0000,0001)。

Scanner sc=Scanner(new File("binary.txt"));

int z=sc.nextInt();

System.out.println(z);

我输出0而不是0000。

3 个答案:

答案 0 :(得分:1)

这是因为您使用的是Integer00000001只是1

您应该使用String代替。

以下是代码段:

Scanner sc = Scanner(new File("binary.txt"));
String z = sc.nextLine();
System.out.println(z);

答案 1 :(得分:1)

int表示0000(即使是基数16)的值为0

如果您想阅读0010并打印16,则需要使用nextInt(16)

如果要保留前导零,请将其读作"0000"字符串。为此,请使用next代替nextInt

答案 2 :(得分:1)

下面是一个简单的HungryScanner课程,它演示了OP的问题以及@Pshemo提供的解决方案以及我自己的两个解决方案。

我发现@ Pshemo的答案令人困惑,尽管已被接受。它给出了两个截然不同的解决方案,OP没有任何迹象表明哪个是好的。

无论如何,其中一个答案(nextInt(16))对我来说似乎不对,因为我理解OP。它会导致扫描程序吃掉下一个十六进制字符序列(在正则表达式中,类似[0-9a-fA-F]+),然后尝试将base-16数字的字符串表示转换为Java int原始。但那不是"二进制," OP似乎想要。

此外,OP代码int z = sc.nextInt()表示需要的是Java int原语,而不是String(@ Pshemo'中提供的第二个策略回答)。这个答案比nextInt(16)更合理,但它似乎不符合要求 - 但 已被接受......

部分OP问题可能与零填充输出值有关(这似乎也是其他答案和评论背后的思考的一部分)。在下面发布的演示代码中,solutionByStevelWithPadding显示了如何轻松生成十进制和十六进制输出的零填充。它还显示了一种为二进制表示进行零填充的技术 - 它不像十进制或十六进制那样简单(没有"%04b"格式 - 叹息...... )。< / p>

由于我已经完成了所有的演示测试和答案写作,尽管已经接受了答案,我也可以发布我的产品。或许OP会澄清,或者其他读者会获得一些东西。

public class HungryScanner
{
    public static void originalProblem(final Scanner scanner)
    {
        System.out.println("\n---originalProblem---\n");

        while (scanner.hasNextLine())
        {
            final int z = scanner.nextInt();

            System.out.println(z + "(dec) = " + Integer.toBinaryString(z) + "(binary) = " + Integer.toHexString(z) + "(hex)");
        }
    }

    public static void solutionByStevel(final Scanner scanner)
    {
        System.out.println("\n---solutionByStevel---\n");

        while (scanner.hasNextLine())
        {
            final int z = scanner.nextInt(2);

            System.out.println(z + "(dec) = " + Integer.toBinaryString(z) + "(binary) = " + Integer.toHexString(z) + "(hex)");
        }
    }

    public static void solutionByStevelWithPadding(final Scanner scanner)
    {
        System.out.println("\n---solutionByStevelWithPadding---\n");

        while (scanner.hasNextLine())
        {
            final int z = scanner.nextInt(2);

            final String extraPadding = ("0000" + Integer.toBinaryString(z));
            final String binaryString = extraPadding.substring(extraPadding.length() - 4);

            System.out.println(""
                + String.format("%02d", z) + "(dec) = "
                + binaryString + "(binary) = "
                + String.format("0x%02x", z) + "(hex)");
        }
    }

    public static void solutionByPshemo(final Scanner scanner)
    {
        System.out.println("\n---solutionByPshemo---\n");

        while (scanner.hasNextLine())
        {
            final int z = scanner.nextInt(16);

            System.out.println(z + "(dec) = " + Integer.toBinaryString(z) + "(binary) = " + Integer.toHexString(z) + "(hex)");
        }
    }

    public static void main(final String[] args) throws IOException
    {
        final String fname = "binary.txt";

        originalProblem            ( new Scanner(new File(fname)));
        solutionByStevel           ( new Scanner(new File(fname)));
        solutionByStevelWithPadding( new Scanner(new File(fname)));
        solutionByPshemo           ( new Scanner(new File(fname)));
    }
}

使用binary.txt内容文件:

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

我得到输出:

---originalProblem---

0(dec) = 0(binary) = 0(hex)
1(dec) = 1(binary) = 1(hex)
10(dec) = 1010(binary) = a(hex)
11(dec) = 1011(binary) = b(hex)
100(dec) = 1100100(binary) = 64(hex)
101(dec) = 1100101(binary) = 65(hex)
110(dec) = 1101110(binary) = 6e(hex)
111(dec) = 1101111(binary) = 6f(hex)
1000(dec) = 1111101000(binary) = 3e8(hex)
1001(dec) = 1111101001(binary) = 3e9(hex)
1010(dec) = 1111110010(binary) = 3f2(hex)
1011(dec) = 1111110011(binary) = 3f3(hex)
1100(dec) = 10001001100(binary) = 44c(hex)
1101(dec) = 10001001101(binary) = 44d(hex)
1110(dec) = 10001010110(binary) = 456(hex)
1111(dec) = 10001010111(binary) = 457(hex)

---solutionByStevel---

0(dec) = 0(binary) = 0(hex)
1(dec) = 1(binary) = 1(hex)
2(dec) = 10(binary) = 2(hex)
3(dec) = 11(binary) = 3(hex)
4(dec) = 100(binary) = 4(hex)
5(dec) = 101(binary) = 5(hex)
6(dec) = 110(binary) = 6(hex)
7(dec) = 111(binary) = 7(hex)
8(dec) = 1000(binary) = 8(hex)
9(dec) = 1001(binary) = 9(hex)
10(dec) = 1010(binary) = a(hex)
11(dec) = 1011(binary) = b(hex)
12(dec) = 1100(binary) = c(hex)
13(dec) = 1101(binary) = d(hex)
14(dec) = 1110(binary) = e(hex)
15(dec) = 1111(binary) = f(hex)

---solutionByStevelWithPadding---

00(dec) = 0000(binary) = 0x00(hex)
01(dec) = 0001(binary) = 0x01(hex)
02(dec) = 0010(binary) = 0x02(hex)
03(dec) = 0011(binary) = 0x03(hex)
04(dec) = 0100(binary) = 0x04(hex)
05(dec) = 0101(binary) = 0x05(hex)
06(dec) = 0110(binary) = 0x06(hex)
07(dec) = 0111(binary) = 0x07(hex)
08(dec) = 1000(binary) = 0x08(hex)
09(dec) = 1001(binary) = 0x09(hex)
10(dec) = 1010(binary) = 0x0a(hex)
11(dec) = 1011(binary) = 0x0b(hex)
12(dec) = 1100(binary) = 0x0c(hex)
13(dec) = 1101(binary) = 0x0d(hex)
14(dec) = 1110(binary) = 0x0e(hex)
15(dec) = 1111(binary) = 0x0f(hex)

---solutionByPshemo---

0(dec) = 0(binary) = 0(hex)
1(dec) = 1(binary) = 1(hex)
16(dec) = 10000(binary) = 10(hex)
17(dec) = 10001(binary) = 11(hex)
256(dec) = 100000000(binary) = 100(hex)
257(dec) = 100000001(binary) = 101(hex)
272(dec) = 100010000(binary) = 110(hex)
273(dec) = 100010001(binary) = 111(hex)
4096(dec) = 1000000000000(binary) = 1000(hex)
4097(dec) = 1000000000001(binary) = 1001(hex)
4112(dec) = 1000000010000(binary) = 1010(hex)
4113(dec) = 1000000010001(binary) = 1011(hex)
4352(dec) = 1000100000000(binary) = 1100(hex)
4353(dec) = 1000100000001(binary) = 1101(hex)
4368(dec) = 1000100010000(binary) = 1110(hex)
4369(dec) = 1000100010001(binary) = 1111(hex)

PS - 在我的演示中,我关闭创建的Scanner对象。 它只是一个演示!! 8&gt; P