File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal start of type

 
jerry cronin
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
current e scanner are duplicate variables.

is not


Remove this first issues and go on.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just seen another mistake

you write



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



 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your try/catch needs to be in a method. You can only have variable declarations outside of a method.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is with tags!

 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my last post
 
J Solomon
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic