Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Illegal start of type?

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Illegal start of type?

    Why does the following code return an "Illegal start of type" compile error?

    import java.io.*;
    import java.util.*;
     
    class AminoToken
    {
        String aminosStr;
        StringTokenizer aminosLn;
        StringTokenizer aminosInf;
        File aminos;
        FileInputStream aminosIS;
     
        try
        {
             aminos = new File("aminos.txt");
             aminosIS = new FileInputStream(aminos);
        }
        catch (Exception e)
        {
     
        }
    }

    My best guess is that, for some reason the compiler isn't recognizing the try keyword, because (as strange as the preceding sounds) all the succeeding compiler error indicate that the compiler is reading the rest of the code as if try were being ignored in every way except that it is an illegal start of type.
    Last edited by mjpam; September 7th, 2010 at 11:35 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Illegal start of type?

    Your try statement isn't inside any method or initializer block. Therefore it must either be a visibility modifier, abstract, variable type or return type.

    Since it is not either of the first two, it must be either a variable type or a return type. However, try is already a reserved keyword, thus making it an illegal type name.

    Try putting your try/catch code inside of a main function.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    mjpam (September 7th, 2010)

  4. #3
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Illegal start of type?

    My original question has been answered, but have another one about the same program and wasn't sure if I should start a new thread or not.

    import java.io.*;
    import java.util.*;
     
    class AminoToken
    {
        //System.out.println("**Inside AminoToken");
        public static void main (String args[])
        {
             //System.out.println("**Inside main");
             char inChar;
             long aminosSize;
             char aminosAry[];
             String aminosStr = "";
             StringTokenizer aminosLn;
             StringTokenizer aminosInf;
             File aminos;
             FileInputStream aminosIS;
     
             //System.out.println("**References created");
     
             try
             {
                   //System.out.println("**Inside try block");
     
                   aminos = new File("aminos.txt");
                   //System.out.println("**File created");
                   aminosIS = new FileInputStream(aminos);
                   //System.out.println("**FileInputStream created");
                   aminosSize = aminos.size();
                   //System.out.println("**aminosSize intialized");
                   aminosAry = new aminosAry[aminosSize];
                   //System.out.println("**aminosAry created");
                   for(int aminosAryInd = 0; aminosAryInd < aminosSize; aminosAryInd++)
                   {
                        aminosAry[aminosAryInd] = (char) aminosIS.read();
                        aminosStr += aminosAry[aminosAryInd];
                        System.out.print(aminosAry[aminosAryInd]);
                   }
             }
             catch (FileNotFoundException fnfe)
             {
                  System.out.println(fnfe);
             }
     
             catch (IOException ioe)
             {
                  System.out.print(ioe);
             }
     
             System.out.println(aminosStr);
        }
    }

    Now, my program compiles just fine, but it appears to hang when I run it. None, of the commented-out, double-asterisked print statements actually print, so I think it's safe to say that the program doesn't "do" anything meaningful,including terminate. I need to force-quit it from the command line.
    Last edited by mjpam; September 7th, 2010 at 11:36 PM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Illegal start of type?

    How are you executing the program? I can't understand how the println()s output don't show on the console.

    Can you show the commandline you use to execute the program?

  6. #5
    Junior Member
    Join Date
    Sep 2010
    Location
    Netherlands
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of type?

    When i copy your code and paste it into Netbeans, Netbeans gives errors at these parts:
     aminosSize = aminos.size(); 
    aminosAry = new aminosAry[aminosSize];

    The errors say that;
    It cannot find the .size in java.io.File
    So, .size isn't in the java class java.io.File

    The other error says that the
    aminosAry = new aminosAry[aminosSize];
    requires an int and finds a long

    --
    If i change the long aminosSize; to int aminosSize; , it will say that it can't find aminosAry
     aminosAry = new aminosAry[aminosSize];


    After Commenting the
     aminosAry = new aminosAry[aminosSize];
    out, i did get output :
     **Inside try block
    **File created
    java.io.FileNotFoundException: aminos.txt (The system cannot find the file specified)
     
    BUILD SUCCESSFUL (total time: 0 seconds)
    --
    Either way, it might be because i copied it
    Last edited by Varial; September 8th, 2010 at 04:21 PM.

  7. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of type?

    Try this:
    package LoopPb;
     
     
    import java.io.*;
    import java.util.*;
     
    class AminoToken
    {
        //System.out.println("**Inside AminoToken");
        public static void main (String args[])
        {
             //System.out.println("**Inside main");
             char inChar;
             long aminosSize;
             char aminosAry[];
             String aminosStr = "";
             StringTokenizer aminosLn;
             StringTokenizer aminosInf;
             File aminos;
             FileInputStream aminosIS;
     
             //System.out.println("**References created");
     
             try
             {
                   //System.out.println("**Inside try block");
     
                   aminos = new File("aminos.txt");
                   //System.out.println("**File created");
                   aminosIS = new FileInputStream(aminos);
                   //System.out.println("**FileInputStream created");
                   aminosSize = aminos.length();
                   //System.out.println("**aminosSize intialized");
                   aminosAry = new char[(int)aminosSize];
                   //System.out.println("**aminosAry created");
                   for(int aminosAryInd = 0; aminosAryInd < aminosSize; aminosAryInd++)
                   {
                        aminosAry[aminosAryInd] = (char) aminosIS.read();
                        aminosStr += aminosAry[aminosAryInd];
                        System.out.print(aminosAry[aminosAryInd]);
                   }
             }
             catch (FileNotFoundException fnfe)
             {
                  System.out.println(fnfe);
             }
     
             catch (IOException ioe)
             {
                  System.out.print(ioe);
             }
     
             System.out.println(aminosStr);
        }
    }

    You should parse file using while..loop, it's safer:
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class ReaddinFile {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		FileReader f = null;
    		try {
    			f = new FileReader("aminos.txt");
    			int c;
    			while ((c = f.read()) != -1) {
    				System.out.print((char) c);
    			}
    		} catch (FileNotFoundException fnfe) {
    			System.out.println("fis abs");
    			fnfe.printStackTrace();
    		} catch (IOException ioe) {
    			System.out.println("Read error");
    			ioe.printStackTrace();
    		} finally {
    			if (f != null) {
    				try {
    					f.close();
     
    				} catch (IOException ioe) {
    					System.out.println("Err inchidere fis");
    					ioe.printStackTrace();
    				}
    			}
    		}
    	}
    }

Similar Threads

  1. Start code again?
    By mortalc in forum Loops & Control Statements
    Replies: 12
    Last Post: May 27th, 2010, 07:26 PM
  2. Illegal start
    By hing09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 25th, 2010, 05:23 PM
  3. Where to start?
    By tonykasdorf in forum Java Theory & Questions
    Replies: 3
    Last Post: March 4th, 2010, 11:52 PM
  4. "illegal modifier for parameter sum; only final is permitted
    By etidd in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2010, 07:51 AM
  5. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2009, 12:57 AM