aspose file tools*
The moose likes Beginning Java and the fly likes Illegal start of type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Illegal start of type" Watch "Illegal start of type" New topic
Author

Illegal start of type

jerry cronin
Greenhorn

Joined: Dec 19, 2012
Posts: 5
Hi, I'm trying to implement a scanner but I'm getting multiple errors that are driving me mad I can't see the problem

I'm gettting "illegal start of type" at the try statement and at the while statement and "class interface or enum expected" at the catch statment
Any help is appreciated


[Added code tags - see UseCodeTags for details]
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
current e scanner are duplicate variables.

is not


Remove this first issues and go on.

Bye,
Nicola
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
I've just seen another mistake

you write



Remember that you should catch an exception in a way like:



Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3645
    
  15
And your try/catch needs to be in a method. You can only have variable declarations outside of a method.


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39390
    
  28
You also want to demonstrate the indentation, which you can do by going back to your code, using the button, and adding code tags to your code. Some keywords can only be used with certain numbers of { between them and the beginning of the class.

And welcome to the Ranch
jerry cronin
Greenhorn

Joined: Dec 19, 2012
Posts: 5
What do you mean "current e scanner are duplicate variables."? I added in (Exception e) as well but im still getting the same error
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

jerry cronin wrote:What do you mean "current e scanner are duplicate variables."? I added in (Exception e) as well but im still getting the same error

Lines 6 and 9 both declare a variable called current.

The biggest problem, though, and the one giving you this particular error, is the one Joanne pointed out.
J Solomon
Ranch Hand

Joined: Oct 08, 2012
Posts: 30
Matthew Brown wrote:
jerry cronin wrote:What do you mean "current e scanner are duplicate variables."? I added in (Exception e) as well but im still getting the same error

Lines 6 and 9 both declare a variable called current.

The biggest problem, though, and the one giving you this particular error, is the one Joanne pointed out.


I think that he is getting confused between declaring and initializing/assigning a variable. You can do this in two separate lines if you'd like, especially if you are going to attempt to set a var to some value that could cause issues (some connection, etc.). Line 6 should just declare the var as follows (you are just making room on the stack):

TinyToken current;

and then line 9 should assign some value to the current reference variable:

current = scanner.nextToken();
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3645
    
  15
J Solomon wrote:Line 6 should just declare the var as follows (you are just making room on the stack):

TinyToken current;

and then line 9 should assign some value to the current reference variable:

current = scanner.nextToken();

As long as he moves line 9 into a method. It won't work where it is at the moment.
jerry cronin
Greenhorn

Joined: Dec 19, 2012
Posts: 5
I'm almost there now, but I'm getting an error at line 9 where i have current "<identifier> expected"
Thanks for all yer help so far as well!

import java.util.Scanner;
import java.io.*;

public class TinyTest
{
TinyToken current;
TinyScanner2 scanner = null;
TinyScanner2 scanner = new TinyScanner2(new FileReader("sample.tny"));
current = scanner.nextToken();

public static void tryCatch()
{
try
{
while(current != null)
{
System.out.printf (
"Token [%s]\n", current.toString());
current = scanner.nextToken();
}
}

catch(Exception e)
{
System.out.printf("error");
}

}



public static void main(String args[])
{
TinyScanner2 tinyScanner = new TinyScanner2().tryCatch();
}

}
jerry cronin
Greenhorn

Joined: Dec 19, 2012
Posts: 5
Here it is with tags!

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3645
    
  15
See my last post
J Solomon
Ranch Hand

Joined: Oct 08, 2012
Posts: 30
jerry cronin wrote:Here it is with tags!



Ok, first off, you need to take a look at what you actually defined and how it should be called. In main(..), you attempt to call tryCatch(..) as if it were a method defined on the TinyScanner2 class, but it is not, it is actually a static method that you've defined your TinyTest class, ex. TinyTest.tryCatch(). But if you call it that way, you will not have access to the variables that you've defined above since they are instance variables (you actually need to create an instance of the TinyTest class for those to exist). I also do not know why you declare the tryCatch(..) method to be static.

In main, you should create an instance of TinyTest, and then call your tryCatch(..) method on the TinyTest instance.
jerry cronin
Greenhorn

Joined: Dec 19, 2012
Posts: 5
I've fixed all the above errors I think, now I'm getting this last one

TinyTest.java:36: incompatible types
found : void
required: TinyTest
TinyTest test = new TinyTest().tryCatch();

Does this mean I must change the return type of tryCatch? And change the way its implemented

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2310
    
  49
I've fixed all the above errors I think, now I'm getting this last one

TinyTest.java:36: incompatible types
found : void
required: TinyTest
TinyTest test = new TinyTest().tryCatch();

Does this mean I must change the return type of tryCatch? And change the way its implemented

Don't just change the method declaration to try to fix a compiler error. If the tryCatch method should return something then change its declaration, if not then change line 36.

Personally I think you should look at line 36 and work out what it does. Hint: should you be doing all that on one line.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal start of type