【问题标题】:if statement to exit from programif 语句退出程序
【发布时间】:2015-03-06 23:33:16
【问题描述】:

假设您有一个二进制文件,其中包含类型为 int 或 double 的数字。您不知道文件中数字的顺序,但它们的顺序记录在文件开头的字符串中。该字符串由字母 i (表示 int)和 d (表示 double)组成,按后续数字的类型顺序排列。字符串是使用 writeUTF 方法写入的。

例如字符串“iddiiddd”表示该文件包含八个值,如下:一个整数,后跟两个双精度数,后跟两个整数,后跟三个双精度数。

我的问题是,如果字符串中的字母多于数字,我如何创建一个 if 语句告诉用户他们试图读取的文件中有错误?

我试过用这个,其中“count”是数字的数量,“length”是字符串的长度,但这不起作用。

    if(count!=length){
        System.out.println("Error in file: Length of string and numbers are not equal");
        System.exit(0);
    }

我的其余代码是这样的:

public static void main(String[] args) {

    Scanner keyboard=new Scanner(System.in);
    System.out.print("Input file: ");
    String fileName=keyboard.next();
    int int_num_check=0;
    double double_num_check=9999999999999999999999999999.999999999;
    int int_num=0;
    double double_num=0.0;
    int count=0;

    try{
        FileInputStream fi=new FileInputStream(fileName);
        ObjectInputStream input=new ObjectInputStream(fi);

        String word=input.readUTF();
        int length=word.length();

        for(int i=0;i<length;i++){

            if(word.charAt(i)=='i'){
                int_num=input.readInt();
                System.out.println(int_num);
                if(int_num>int_num_check){
                    int_num_check=int_num;
                }
            }

            else if(word.charAt(i)=='d'){
                double_num=input.readDouble();
                System.out.println(double_num);
                if(double_num<double_num_check){
                    double_num_check=double_num;
                }
            }

            else{
                System.out.println("Error");
                System.exit(0);
            }
            count++;

        }

        System.out.println("count: "+count);
        System.out.println("length "+length);

        if(count!=length){
            System.out.println("Error in file: Length of string and numbers are not equal");
            System.exit(0);
        }

         String checker=input.readUTF();
            if(!checker.equals(null)){
                System.out.println("Error");
                System.exit(0);
            }

        input.close();
        fi.close(); 

}
    catch(FileNotFoundException e){
        System.out.println("Error");
        System.exit(0);
    }
    catch(EOFException e){

        System.out.println("Largest integer: "+int_num_check);
        System.out.println("Smallest double: "+double_num_check);
        System.exit(0);
    }
    catch(IOException e){
        System.out.println("Error");
        System.exit(0);
        }


}

}

【问题讨论】:

  • 除非我弄错了,否则当没有更多要阅读的内容时,您的应用程序将在input.readInt();(或双倍)处抛出异常。发生这种情况时,您可以捕获异常并进行处理。
  • 我已经包含了我能想到的所有异常,只是当我测试这个程序并期望出现错误时我没有得到错误,我想知道为什么我没有得到一个?
  • 你能发布文件的内容吗?
  • 文件显示“idid 8 4.33316 2”@MateusViccari
  • 它是用 ObjectOutputStream 编写的。它不是我自己写的,它是给我的,用于为即将到来的测试练习我的代码@MateusViccari

标签: java if-statement binaryfiles


【解决方案1】:

如果字母多于数字,您可能已到达文件末尾 (eof)。在每个文件读取一个数字后检查 eof,如果在读取所有预期数字之前到达 eof,则报告错误。

【讨论】:

  • 但我不是已经这样做了吗?还是我做错了?
【解决方案2】:

我认为问题可能出在您的文件的创建方式上。尝试使用以下代码创建它:

try {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:/temp/file.txt"));
    oos.writeUTF("idid");
    oos.writeInt(8);
    oos.writeDouble(4.33316);
    oos.writeInt(2);
    oos.flush();
    oos.close();
} catch (Exception ex) {
    ex.printStackTrace();
}

然后使用您的代码读取它,它应该会抛出 EOFException。

【讨论】:

  • 好的,我也试试这个。谢谢@MateusViccari
【解决方案3】:

如果您有更多的整数/双精度字母,您的代码将转到EOFException 的捕获点,但是您没有关于 EOF 的代码,但您出于某种原因打印了最大/最小值。

因为只有当你的字母多于数字时才会出现 EOF 异常(因为你是根据word 的长度进行读取的),所以你应该移动到里面来捕捉你的

System.out.println("Error in file: Length of string and numbers are not equal");
System.exit(0);

【讨论】: