Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从程序中退出的if语句_Java_If Statement_Binaryfiles - Fatal编程技术网

Java 从程序中退出的if语句

Java 从程序中退出的if语句,java,if-statement,binaryfiles,Java,If Statement,Binaryfiles,假设您有一个二进制文件,其中包含类型为int或double的数字。您不知道文件中数字的顺序,但它们的顺序记录在文件开头的字符串中。该字符串由字母i(表示int)和d(表示double)组成,顺序为后续数字的类型。使用writeUTF方法写入字符串 例如,字符串“iddiidd”表示文件包含八个值,如下所示:一个整数,后跟两个双精度,后跟两个整数,后跟三个双精度 我的问题是,如果字符串中的字母多于数字,我如何创建一个if语句,告诉用户他们试图读取的文件中有错误 我试着使用它,其中“count”是数

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

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

我的问题是,如果字符串中的字母多于数字,我如何创建一个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);
        }


}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入文件:”);
字符串文件名=keyboard.next();
int\u num\u check=0;
双双_num_check=999999999999999999999.999999999;
int_num=0;
double-double_num=0.0;
整数计数=0;
试一试{
FileInputStream fi=新的FileInputStream(文件名);
ObjectInputStream输入=新ObjectInputStream(fi);
String word=input.readUTF();
int length=word.length();
对于(整数i=0;整数检查){
int_num_check=int_num;
}
}
else if(word.charAt(i)='d'){
double_num=input.readDouble();
System.out.println(双_num);

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

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

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。

如果你有更多整数/双倍的字母,你的代码将转到
EOFException
的catch,但是你没有关于EOF的代码,但是出于某种原因,你打印了最大/最小的值

由于只有当字母多于数字时,EOF异常才会上升(因为您是根据
单词的长度进行读取的),因此您应该进入捕捉字符的内部

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

除非我弄错了,否则应用程序将在
input.readInt();
(或double)处抛出异常当没有更多的东西要读时。你可以在发生这种情况时捕获异常并处理它。我已经包括了我能想到的所有异常,只是当我测试这个程序并期望出现错误时,我没有得到错误,我想知道为什么我没有得到错误?你能发布文件的内容吗?文件上说“idid 8 4.33316 2”@MateusViccari它是用ObjectOutputStream编写的。我不是自己编写的,而是让我为即将到来的测试@MateusViccari练习代码的。但我现在还没有这样做吗?或者我做得不对吗?好的,我也会尝试一下。谢谢@MateusViccari