illiegal start of type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    illiegal start of type

    Hi all, i am getting a coupke of compile errors with this code, any ideas - stuck on it for a good few hours now.

    im getting a identifier in line 23 expected
    }

    and

    illiegal start of type
    try
    ^

    Any help is appreciated.




    [code=java]
    import java.io.*;
    import java.awt.*;
    import java.util.*;

    public class FileWriter2
    {
    String pathName = "C:\\BlueJ\\bre n.txt";
    File aFile = new File(pathName);


    try
    {

    FileWriter aFileWriter = new FileWriter(aFil e);

    aFileWriter.wri te("Test");
    aFileWriter.clo se();
    }
    catch (Exception anException)
    {
    System.out.prin tln("error :" + anException)
    }
    }
    [/code]
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Don't forget that code has to go into methods -- you forgot to define any methods, for example, a main method.

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      put it in a main maethod and now i m getting five errors - including it doesn't find symbol: method close and write.

      Code:
      import java.io.*;
      
      
      
      public class FileWriter2
      {
      public static void main(String[] args)
      {
      String  pathName = "C:\\BlueJ\\bren.txt";
      File aFile = new File(pathName);
      
      
      try
      {
      
      FileWriter aFileWriter = new FileWriter(aFile);
      
      aFileWriter.write("Test");
      aFileWriter.close();
      }
      catch (Exception anException)
      {
      System.out.println("error :" + anException);
      }
      }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You're not showing us the code you fed to your compiler.

        kind regards,

        Jos

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          javac FileWriter2.jav a

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by brendanmcdonagh
            javac FileWriter2.jav a
            That's not what I mean; your code in reply #3 compiles fine for me so I was a bit
            surprised that compilation failed in your case.

            kind regards,

            Jos

            Comment

            • brendanmcdonagh
              New Member
              • Nov 2007
              • 153

              #7
              Glad to hear what im doing is right, but why won't it compile??

              I have dona a print screen on cmd errors after javac FileWriter222.j ava if anyone has the time to have a look but don't know how to attach file

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by brendanmcdonagh
                Glad to hear what im doing is right, but why won't it compile??

                I have dona a print screen on cmd errors after javac FileWriter222.j ava if anyone has the time to have a look but don't know how to attach file
                Run it again, redirect the compiler's output streams to a file and copy/paste the
                content to a new message; I'm sort of curious why that code won't compile ...

                kind regards,

                Jos

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  <adjusting mindreader's turban/>
                  Did you earlier define a FileWriter.java ? If so, the compiler is confusing it with java.io.FileWri ter. Delete both your FileWriter.java and FileWriter.clas s.

                  Comment

                  • brendanmcdonagh
                    New Member
                    • Nov 2007
                    • 153

                    #10
                    Originally posted by BigDaddyLH
                    <adjusting mindreader's turban/>
                    Did you earlier define a FileWriter.java ? If so, the compiler is confusing it with java.io.FileWri ter. Delete both your FileWriter.java and FileWriter.clas s.

                    Yes!!

                    No i get a noclassdeffound error exception in thread main - but ill prob be able to work that out unless someone can see it quickly - reply 3

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by brendanmcdonagh
                      Yes!!

                      No i get a noclassdeffound error exception in thread main - but ill prob be able to work that out unless someone can see it quickly - reply 3

                      it's hard to see errors when the code we have has none ;-(

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by BigDaddyLH
                        it's hard to see errors when the code we have has none ;-(
                        Yep, also see reply #6; the OP's question is quite obscure and sloppy; there are
                        external conditions here that are neither realized nor mentioned by the OP. We
                        have to dust off our crystal balls ...

                        kind regards,

                        Jos

                        Comment

                        • brendanmcdonagh
                          New Member
                          • Nov 2007
                          • 153

                          #13
                          Sorry guys,

                          Because (as someone suggested) I had similar named files in directory when i type javac i got earlier mentioned errors, i deleted these files and this enable compilation. the runtime error was my mistake but thank you so much for taking the time to help me:)

                          Comment

                          Working...