• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Illegal start of type

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just seen another mistake

you write



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



 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is with tags!

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See my last post
 
J Solomon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic