Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes illegal start of an expression 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 an expression" Watch "illegal start of an expression" New topic
Author

illegal start of an expression

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am putting together this program, when I compiled it I got the same error message over and over again:

Illegal start of an expression

and also:

unexpected character

These are pointing to places in code that I am actually very comfortable with and know that those parts are set up correctly. Maybe someone can find where I have gone wrong in my code?



I appreciate any help in the matter
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
Hi Ben

Well... without having the line-numbers, the error points to, it's much more difficult to find out...

But my guess are these lines (26, 28, 32, 34):



Check them again .

Stefan
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Could you please print the Stack Trace of the compiler error!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Ben,

A piece of advice - when you get a compiler error and want to ask about it, please post the WHOLE erorr, exactly as it appears. there's TONS of info there once you learn how to read it. It tells you the line number and sometimes even the position on the line where it thinks the error is. without all that, readers either have to guess where the problem is, or copy your code to their machines and try compiling it themselves.

The easier you make it form someone to help you, the more likely you are to get that help.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Sorry about the lack of info, on the other hand the suggestions to look over the 4 print lines let me notice I forgot to add the + to append the variable and string together. A lot less errors now, thanks a lot for that.

The ones I am getting now refer to lines: 27 and 30

Says it cant find symbol and refers to the . adding the subString method on both lines

says the symbol is subString method

do I need to import the API class to use it? I thought java.lang was automatically in there
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Ben:

You need to add plusses (+) in your println() calls after the initial String literals, so

should be:

Also, your call to substring() should be in all lowercase. Also, you'll need to adjust your arguments, since a two-argument substring takes the specified beginning character position, up to (but not including) the specified ending character position.

John.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Ok so here is what I am getting now. I changed the subString to substring, fixed that. I get the error below.




I change currentIndex to a String, then I get these errors below




Not really sure what to make of this: I changed currentIndex to a String from an int to match phoneNumber data type, the reason I have phoneNumber as a String and not an int is because we are suppose to assume there will be dashes in the phone Number. I am guessing int will not accept dashes, so I declared it as a String. Am I wrong here?

My current program looks like this:




Thanks a for the help
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
Phone number (phoneNumber) as a string is quite ok. And the index (currentIndex) should indeed be an int.

The error lays elsewhere: Look at the return value of the String.substring(...)-method. The variable, you assign the return value to, should be of the same type.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the help, I made the adjustments and read over the sun explanation of substring method. I just have one question.

1. the variable of the return value, is its type suppose to match phoneNumber or currentIndex?

Thanks a lot for your help
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
Neither, nor. You assign the return value to localNumber, don't you?

substring()'s return value type is String. The variable you assign this value to must therefore also be a String. D'accord?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Do you remember the substitution principle from math? if a = b, you can replace every 'a' with 'b', and things are still ok. it's the same in java. When the API says 'this method returns a String', you can effectively treat the method as a String. In fact, you almost HAVE to.

In your code, you wrote this:

and we know that 'substring' returns a string. so effectively, we have

and localNumber is defined as an 'int', effectively giving this:

You can't assign a String to an int. That is what the compiler is telling you.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks a lot for all your help, it was the localNumber that needed to be a String. Works great
 
Consider Paul's rocket mass heater.
 
subject: illegal start of an expression
 
Similar Threads
calling a method w/out an object
Need help Getting error "illegal error:illegal start of expression"
variables may not be initialized
cannot find symbol inside String?
Java Validation! Field should be Integer