I have a problem with the error illegal start of expression

Hi,

i am having problems with the illegal start of expression error when I add some methods to my class. I thought I have checked everything but it definitly doesn't work. I don't know what to do. Please help.

Please find below my code.

Thank you very much.

public class Methodensammlung {

/**

* Wir geben bei der Objekterzeugung eine Meldung aus

*/

public Methodensammlung()

{

System.out.println("Object has been constructed")

}

/**

* @return the next natural number

*/

public void nextNumber(int x)

{

return x+1;

}

/**

* @param n how often to bark?

*/

public void bark(int n)

{

for (i=0; i!=n; i++)

System.out.println("wuff!");

}

/**

* @return the number as a word, if it is 1,2 or 3*/

public String oneTwoThree(int x)

{

String s;

switch (x)

{

case 1: s = "one";

case 2: s = "two";

case 3: s = "three";

default: s = "invalid!";

}

return s;

}

/**

* Gibt "Hello World!" auf der Konsole aus.

*/

public void helloWorld()

{

System.out.print1n("Hello World!");

}

/**

* @return true, falls die uebergebene Zahl 0 ist

*/

public boolean istNull(int x)

{

if (x=0)

return true;

else

return false;

}

/**

* Haengt fuenf Ausrufezeichen an den String an

*/

public void ausrufezeichenAnhaengen(String s)

{

s = s + "!!!!!";

}

/**

* @return true, if x is between 0 and 9

*/

public boolean istZiffer(int x)

{

return (0 <= x <= 9);

}

/**

* Das Alphabet auf dem Bildschirm ausgeben

*/

public void alphabet()

{

for (char buchstabe='a'; buchstabe<='Z'; ++buchstabe)

System.out.print(buchstabe);

}

/**

* Laeuft zehnmal durch die Schleife durch

*/

public void zehnDurchlaeufe()

{

for (float x=17000000f;

x<17000010f; ++x);

}

/**

* @return the sum of the first 20 natural numbers

*/

public int sum20()

{

private int s = 1+2+3+4+5+6+7+8+9+10;

s = s +11+12+13+14+15+16+17+18+19+20;

return s;

}

/**

* Falls die uebergebenen Referenzen beide null sind

* oder die referenzierten String-Objekte gleich sind,

* dann wird eine entsprechende Meldung ausgegeben.

*/

public void namensBrueder(String a, String b)

{

if (a==null && b==null) || (a.equals(b))

{

System.out.println("So ein Zufall!");

}

}

/**

* Hatten Sie diesmal Glueck? :)

*/

public void 7aus49()

{

System.out.println("Die Glueckszahlen lauten: ");

System.out.println("1, 3, 5, 20, 32, 36, 40.");

}

/**

* @return true, falls s "+" oder "-" ist.

*/

public boolean istOperator(String s)

{

/* Wir wollen zunaechst nur + und - zulassen

* String ops = "+-*/%^~";

*/

String ops = "+-";

return (s.length()==1 && ops.contains(s));

}

/**

* @return das Quadrat von X

*/

public int quadrat(int X)

{

return x*x;

}

/**

* @return die vorherige natuerliche Zahl

*/

public int vorgaenger(int x)

{

return x--;

}

/**

* @return -1 (negativ), +1 (positiv), 0 (sonst)

*/

public int vorzeichen(int x)

{

int v;

if (x<0)

v = -1;

else

v = +1;

else

v = 0;

return v;

}

/**

* Eine hoefliche Bitte

*/

public void zahnarzt()

{

System.out.println("Bitte einmal "Aaaaa" sagen!");;;

}

/**

* @return die groesste der drei angegebenen Zahlen

*/

public int max(int a, b, c)

{

// Math.max liefert das Maximum zweier Zahlen

return Math.max(a, Math.max(b, c));

}

/**

* @return 20, falls x kleiner als 20 ist, ansonsten x

*/

public int mindestens20(int x)

{

return Math.min(x, 20);

}

}

[4286 byte] By [JustLilla] at [2007-11-26 14:52:50]
# 1
1) Your code is not formatted2) You're just dumping it on us to cut-n-paste and compile for you to see the error message(s) you're already seeing but choosing (for whatever reason) to not disclose hereNo thanks
warnerjaa at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 2
http://forum.java.sun.com/help.jspa?sec=formattingAnyway. Illegal start of expression means either- processing code outside any methods or c'tors or initializer blocks- missing parenthesis- missing bracesCheck for those.
CeciNEstPasUnProgrammeura at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 3
> System.out.println("Bitte einmal "Aaaaa" sagen!");;;You need to escape the inner quotes using a backslash: \"...
CeciNEstPasUnProgrammeura at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 4
What my friend warner here is trying to say is please repost your code between [code][/code] tags and give us the contents of the error messages and point out which line it refers to.
hunter9000a at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 5

Look at the error message provided by the compiler -- it SHOULD give you the line number of the error. That would go a long way to helping you (and us) figure out where the mistake is.

It's definitely a syntax error, and it typically implies that you're missing a closing brace or bracket, or maybe a semicolon.

- Adam

guitar_man_Fa at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 6

> public class Methodensammlung {

> /**

> * Wir geben bei der Objekterzeugung eine Meldung aus

> */

> public Methodensammlung()

> {

> System.out.println("Object has been constructed")

> }

first problem ins right there: there should be a semi-colon after the system.out.println statement. i.e.

public Methodensammlung() {

System.out.println("Object has been constructed"); // <-- note the semicolon

}

- Adam

guitar_man_Fa at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 7

Ooh ooh! No breaks in your switch statement!

switch (x)

{

case 1: s = "one";

case 2: s = "two";

case 3: s = "three";

default: s = "invalid!";

}

s will always be "invalid" no matter what x is.

hunter9000a at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 8

> Ooh ooh! No breaks in your switch statement!

>

> switch (x)

> {

> case 1: s = "one";

> case 2: s = "two";

> case 3: s = "three";

> default: s = "invalid!";

> }

>

> s will always be "invalid" no matter what x is.

true, but that's a semantic error, not a syntax error. (just nitpicking...)

guitar_man_Fa at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 9
This is another case of "don't write the entire program at once, then compile". Rather write a little, make it compile, add some more, make it compile, repeat.
CeciNEstPasUnProgrammeura at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 10

> true, but that's a semantic error, not a syntax

> error. (just nitpicking...)

Yes, maybe I should stick to getting the code compiled before fixing bugs :)

How about this, missing an open parentheses after if and an extra one before a.equals

if (a==null && b==null) || (a.equals(b))

{

System.out.println("So ein Zufall!");

}

hunter9000a at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 11
And finally, else without ifif (x<0)v = -1;elsev = +1;elsev = 0;
hunter9000a at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 12

Hey guys.

I have to say you are really great !

Very fast and very helpful !

Thank you very much !

And of course you are right, it is better to make everything step by step..

The first problem I am still having is the following :

/**

* @return true, falls die uebergebene Zahl 0 ist

*/

public boolean istNull(int x)

{

if (x=0)

return true;

else

return false;

}

When I compile the system tells me that it found int but expected boolean. soo.. what to do ?

JustLilla at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 13
(x == 0) - comparisions always using == (or equals(), but not here)
CeciNEstPasUnProgrammeura at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 14
By the way, considerreturn x == 0;;)
CeciNEstPasUnProgrammeura at 2007-7-8 8:41:05 > top of Java-index,Java Essentials,Java Programming...
# 15

> > true, but that's a semantic error, not a syntax

> > error. (just nitpicking...)

>

> Yes, maybe I should stick to getting the code

> compiled before fixing bugs :)

>

> How about this, missing an open parentheses after if

> and an extra one before a.equals

> if (a==null && b==null) || (a.equals(b))

> {

> System.out.println("So ein Zufall!");

> }

yep. that's definitely a syntax error.

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 16

> Hey guys.

>

> I have to say you are really great !

> Very fast and very helpful !

> Thank you very much !

>

> And of course you are right, it is better to make

> everything step by step..

>

> The first problem I am still having is the following

> :

>

> > /**

> * @return true, falls die uebergebene Zahl 0 ist

> */

> public boolean istNull(int x)

> {

> if (x=0)

> return true;

> else

> return false;

> }

>

>

>

>

> When I compile the system tells me that it found int

> but expected boolean. soo.. what to do ?

the reason is because "x=0" is an assignment, and because of assignment chaining, x=0 actually returns 0 (an integer), so that I could do something like this:

x = y = z = 0; (which assigns 0 to x, y, and z).

what you really wanted is (x == 0) (two equals signs). That returns a boolean comparison.

By the way, that sort of method is often more simply written like this:

public boolean istNull(int x) {

return (x == 0);

}

I mean, honestly, what your saying is, if comparison is true, return true, otherwise (if comparison is false) return false.

You might as well say: return whether comparison is true.

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 17

oh **** .. you are so right ;) and I am so blind .. sometimes.

Do you know what is wrong with this ? :

public void nachfolger(int x)

{

return x+1;

}

JustLilla at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 18

> oh **** .. you are so right ;) and I am so blind ..

> sometimes.

>

> Do you know what is wrong with this ? :

>

> public void nachfolger(int x)

> {

>return x+1;

> e]

yeah, I do.

The signature of that method has a return type of void -> that means it isn't expecting ANYTHING to be returned.

If you really want it to return x+1, then you need to change the signature to:

[code] public int nachfolger(int x)

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 19

Ah .. of course it has to be int not void. Thank you adam.

Another problem :

/**

* @return true, if x is between 0 und 9

*/

public boolean istZiffer(int x)

{

if (0 <= x <= 9)

return x;

}

System tells me that operator <= cannot be applied to boolean, int

JustLilla at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 20

> Ah .. of course it has to be int not void. Thank you

> adam.

>

> Another problem :

>

>

> /**

> * @return true, if x is between 0 und 9

> */

> public boolean istZiffer(int x)

> {

> if (0 <= x <= 9)

> return x;

> }

>

> System tells me that operator <= cannot be applied to

> boolean, int

Unfortunately, you can't chain comparison operators in this way. You will have to do this:

if (x >= 0 && x <= 9)

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 21

> > Ah .. of course it has to be int not void. Thank

> you

> > adam.

> >

> > Another problem :

> >

> >

> > /**

> > * @return true, if x is between 0 und 9

> > */

> > public boolean istZiffer(int x)

> > {

> > if (0 <= x <= 9)

> > return x;

> > }

> >

> > System tells me that operator <= cannot be applied

> to

> > boolean, int

>

> Unfortunately, you can't chain comparison operators

> in this way. You will have to do this:

>

> if (x >= 0 && x <= 9)

>

> - Adam

The reason for this, by the way, is that java would evaluate this as follows:

0 <= x <= 9

(0 <= x) <= 9

(true) <= 9OR(false) <= 9

These last two options, are, of course, nonsense...

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 22
Jeepers, take a class will ya! This (attempting to learn syntax via the forum message by message) is going to take forever and be very painful.
warnerjaa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 23

> Jeepers, take a class will ya! This (attempting to

> learn syntax via the forum message by message) is

> going to take forever and be very painful.

It's not so much about taking a class to learn the syntax. It's more about being able to decipher the compiler error messages, which isn't as trivial when you first start as it is when you have the amount of experience that you do, W.

I'm sure that most of us here tried to chain greater than, and less than operators in some programming language at some point, and had to discover the hard way that you can't do that, and I don't recall having ever been told that by my prof...

It's an honest mistake. In fact, looking at the source code that was posted, I am reminded quite a bit of my first foray into java -> the intent is there, and the syntax IS generally there too... it just has a few mistakes, which, through time, the OP will learn to recognize on his/her own.

until then, would it really hurt to explain what the error messages mean?

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 24
oh you are really great adam ! I wish I were as smart as you are ;)
JustLilla at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 25

> oh you are really great adam ! I wish I were as smart

> as you are ;)

you'll get there, eventually. It's not so much that I'm smart, as that I've had a lot of experience. And there are better programmers here than me, by far. Warnerja, for example, is a better programmer than I am, but he just doesn't seem to have the patience that I do, or else, chooses not to exercise that patience.

- Adam

guitar_man_Fa at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...
# 26
I just can say that I am very greatful for your help and patience !Actually it is a hard way to learn java for the first time. But I'll give my best ,)
JustLilla at 2007-7-21 16:20:24 > top of Java-index,Java Essentials,Java Programming...