【问题标题】:error: reached end of file while parsing - java错误:解析时到达文件末尾 - java
【发布时间】:2014-02-10 03:25:18
【问题描述】:

所以我正在为我的 java 课做作业。我正在尝试编写一个将 4 字节二进制数转换为十进制的简单程序。现在,我承认我使用了通过搜索找到的一些行。所以,我不太确定我的代码发生了什么。我的来源如下。

import java.util.Scanner;

public class B2Dconversion
{
    public static void main (String [] args)
    { 

    System.out.println("Hello, I am so glad you stopped by my little place I call home.");
    System.out.println("I have been told I have many talents, but the one that I can do for you now.");
    System.out.println("What I would like you to do is enter a 4-byte binary number and I will convert it for you into decimal.");
    try 
            {                                    //thread to sleep for the specified number of milliseconds
            Thread.sleep(5000);
            } catch (java.lang.InterruptedException ie) {
            }
    System.out.println("..... Oh you don't know what a 4-byte binary number is?");
    System.out.println("That is just a 4 digit number consisting of any combination of 1's and 0's.");
    System.out.println("i.e. 0001 or 1111 or 1010 etc...");
    System.out.println("So go ahead and enter any 4 digit number in the space below and I will convert it to its binary counterpart.");

    int binary1, decimal1; // declaration of the integer variables

    binary1 = keyboard.nextLine();
    decimal1 = Interger.parseInt(binary1,2); // converts binary to decimal see java documentation at http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29

    System.out.println("You entered " + binary1); // confirmation of the input
    System.out.println("Abra-Cadabra");
    System.out.println("Bippity Boppity");
    System.out.println("Beep, Bop, Boop, eeerrrrr");

    System.out.println("Now that is done the results are ");
    System.out.println( + decimal1);
    }

编译时出现 1 个错误,唯一显示的是 B2Dconversion.java:42: 错误:解析时到达文件末尾

我能得到的任何帮助都会很棒。

【问题讨论】:

    标签: java binary


    【解决方案1】:

    你没有平衡括号。这就是你得到那个错误的原因。您必须在文件末尾使用 } 字符关闭课程。如果你用谷歌搜索了那个错误。你可以在 stackoverlow 上找到类似的问题

    Reached End of file while parsing

    Java compile error: "reached end of file while parsing }"

    【讨论】:

    • 感谢您的关注。现在为了真正的乐趣....不过我有一个新错误。 'B2Dconversion.java:34: 错误: 找不到符号 d1 = Interger.parsInt(b1,2);符号:变量 整数位置:类 B2Dconversion' 我必须进行一些更改,所以这是我相信它正在引用的代码的 sn-p。诠释 b1, d1; // 整数变量声明 Scanner keyboard = new Scanner(System.in); b1 = 键盘.nextInt(); d1 = Interger.parseInt(b1,2);
    • d1 simbol 不在您发布的代码中。这很奇怪。实际错误是什么?
    • 对不起我修改了代码以简化变量,
    • 您没有定义“键盘”。使用前定义
    • 这不是定义“键盘”吗?扫描仪键盘 = new Scanner(System.in);
    【解决方案2】:

    所以这就是我最终为使程序正常工作而做的事情

    import java.util.Scanner;
    public class Ch2PP11BinaryToDecimal
    {
        public static void main (String [] args)
        { 
    
        System.out.println("Hello, I am so glad you stopped by my little place I call home.");
        System.out.println("I have been told I have many talents, but the one that I can do for you now.");
        System.out.println("What I would like you to do is enter a 4-byte binary number and I will convert it for you into decimal.");
        try 
                {                                    //thread to sleep for the specified number of milliseconds
                Thread.sleep(5000);
                } catch (java.lang.InterruptedException ie) {
                }
        System.out.println("..... Oh you don't know what a 4-byte binary number is?");
        System.out.println("That is just a 4 digit number consisting of any combination of 1's and 0's.");
        System.out.println("i.e. 0001 or 1111 or 1010 etc...");
        System.out.println("So go ahead and enter any 4 digit number in the space below and I will convert it to its binary counterpart.");
    
        String binaryNumber;
        int eights, fours, twos, ones, decimalNumber; // declaration of the integer variables
    
        Scanner keyboard = new Scanner(System.in);
    
        binaryNumber = keyboard.nextLine();
        decimalNumber = 0;
        decimalNumber = decimalNumber+(binaryNumber.charAt(0)-'0')*8;
        decimalNumber = decimalNumber+(binaryNumber.charAt(1)-'0')*4;
        decimalNumber = decimalNumber+(binaryNumber.charAt(2)-'0')*2;
        decimalNumber = decimalNumber+(binaryNumber.charAt(3)-'0')*1;
    
        System.out.println("You entered " + binaryNumber); // confirmation of the input
        System.out.println("Abra-Cadabra");
        System.out.println("Bippity Boppity");
        System.out.println("Beep, Bop, Boop, eeerrrrr");
        try 
                {                                    //thread to sleep for the specified number of milliseconds
                Thread.sleep(5000);
                } catch (java.lang.InterruptedException ie) {
                }
        System.out.println("Now that is done the results is ");
        System.out.println(decimalNumber);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多