开发者

try to end a program if file no found

开发者 https://www.devze.com 2023-02-17 06:09 出处:网络
I am trying to terminate a program if the if statement returns that the file does not exist. At the moment the method is

I am trying to terminate a program if the if statement returns that the file does not exist. At the moment the method is

public static void calcMarks() 
  {
      String Assessment1 = null, Assessment2 = null, Assessment3 = null;
      int mark1 = 0,mark2 = 0,mark3 = 0,sum = 0;
       try {
       File doc = new File ("marks.txt");
       if(!doc.exists())
       {
        System.out.println ("Marks.txt Does Not Exist");
        System.exit(); 
       }
       if(!marks.exists())
       {
         System.out.println ("TotalMarks.txt Does Not Exist");
         System.exit(); 
       }
       Scanner input = new Scanner(doc);
         while (input.hasNext()){
          Assessment1 = input.next();
          mark1 = input.nextInt();
          Assessment2 = input.next();
          mark2 = input.nextInt();
          Assessment3 = input.next();
          mark3 = input.nextInt();

        }  
      input.close();
      sum = mark1 + mark2 + mark3;
      System.out.println(Assessment1 + " "  +mark1  +"\n"+  Assessment2  + " "+mark2 +"\n"+  Assessment3  + " "+mark3+ "\n" + "Total" +""+ "=" + sum);
    }
    catch (FileNotFoundException ex) {
      System.err.println("File has not been found");
    } 

     }

but on the System.exit(); is the if statement, I get the error exit(int) in Jaba.lang.system. cannot be applied to ()

I h开发者_StackOverflow社区ave no idea what that means


You need to pass the return value in the exit() method. Optimally, you should have System.exit( 0 );.

You can read this.


You must pass a status code (as an integer) to the System.exit() method. There is no argument-less version of this method.

The method's documentation:

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. This method calls the exit method in class Runtime. This method never returns normally.

Parameters: status - exit status.


You need to pass a return code to exit. Have a look at the documentation for java.lang.System. System.exit(-1); should work.


you need to enter an integer as a parameter for System.exit.

System.exit(0);

or

System.exit(1);  


I'm not a Java programmer, but it looks to me like System.exit() should have an exit code. Try this:

System.exit(0);


System.exit(int) requires an integer argument:

System.exit(1);

The operating system is expecting an Exit Status to report to the program that spawned your program.

A further nitpick: you are looping over input, storing it into variables, and then doing nothing with those variables until the end of your loop. You will throw away all but the last input to your routine. So when you go looking for that bug, you'll know where to look. :)


If I remember right with java an argument must be passed to. This serves as a debugging tool so you can easily tell what part forced the program to exit. without having to print anything out. Generally one would use 0 for any successful exit and any other predetermined values by the programmer as an "error code." So without any further explination you would want to change your exit call to something like the following:

System.exit(0); //This would be for a programming successfully running and exiting.
or
System.exit(X); //Where x is any int to signal a program failure
0

精彩评论

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

关注公众号