1

I get the following error message 'llegal start of expression' at line

throws FileNotFoundException

I have done some research but wasn't able to fix it. Can you help? Many thanks, zan

import java.io.FileNotFoundException;
    import java.io.File;
    import java.util.Scanner;
    import static java.lang.System.out;

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

            throws FileNotFoundException{

            Scanner diskScanner = new Scanner(new File("occupancy"));

            out.println("Room\tGuests");

            for(int roomNum = 0; roomNum < 10; roomNum ++){
                out.print(roomNum);
                out.print("\t");
                out.println(diskScanner.nextInt());
                }
            }
        }
    }
zan
  • 95
  • 1
  • 8
  • What are you trying to do? Declare that main can throw that exception? Or are you looking for a try-catch syntax? – Corbin Oct 08 '11 at 11:17

3 Answers3

3

You shouldn't have a curly brace before the throws keyword :

public static void main(String[]args) throws FileNotFoundException {
                                     ^-- no curly brace

Note that a class should always start with an uppercase letter.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

Improper use of throws.

public static void main(String[]args) throws FileNotFoundException
{
 ..
}

It is better to use try..catch

public static void main(String[]args) 
    {
      try
       {
        ..
       }catch(FileNotFoundException ex)
        {
          //
        }
    }
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • The code with the try/catch block does basically the same thing, except it's longer, and exits with 0 (success) rather than 1 (error). I would argue that the code with the throws clause is better. – JB Nizet Oct 08 '11 at 11:30
  • @JBNizet Agree with current code snippet but what happen when method contain other statements? In OP post, he/she has to handle **NoSuchElementException** too. – KV Prajapati Oct 08 '11 at 11:39
  • He could also add this exception to the throws clause. There is no need to catch an exception if it's just to print its stack trace and exit. – JB Nizet Oct 08 '11 at 11:41
  • 1
    Please, *please*, do not call `printStackTrace` and continue as if nothing happened: http://stackoverflow.com/q/7469316/238421, http://onjava.com/onjava/2003/11/19/exceptions.html – Danilo Piazzalunga Jan 19 '12 at 21:47
-1

Your syntax is wrong. Check your source and/or the language specification.

user207421
  • 305,947
  • 44
  • 307
  • 483