Chris
Chris

Reputation: 211

try to end a program if file no found

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 have no idea what that means

Upvotes: 0

Views: 2447

Answers (7)

user499054
user499054

Reputation:

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

You can read this.

Upvotes: 3

oddenodin
oddenodin

Reputation: 71

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

Upvotes: 0

sarnold
sarnold

Reputation: 104110

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. :)

Upvotes: 0

Ken White
Ken White

Reputation: 125757

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

System.exit(0);

Upvotes: 0

matt b
matt b

Reputation: 140061

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.

Upvotes: 1

MBU
MBU

Reputation: 5098

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

System.exit(0);

or

System.exit(1);  

Upvotes: 0

AdamH
AdamH

Reputation: 2201

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

Upvotes: 0

Related Questions