Syntax error

Discussion in 'Java' started by Patrick Vanhoof, Apr 20, 2004.

  1. Hi,

    I am pretty new to Java, and am therefor already blocked by a simple program
    (code avalaible below). The last line of the program (the println) gives a
    syntax error:
    'Syntax error on token "(", "Identifier" expected'
    Anyone knows why I get this message?

    public class Storage {
    Storage st = new Storage();
    String str = new String("dit is een string");
    public static void main(String[] args) {
    }
    int storage(String s) {
    return s.length() * 2;
    }
    System.out.println(st.storage(str));
    }


    Thanks in advance,
    Patrick
     
    Patrick Vanhoof, Apr 20, 2004
    #1
    1. Advertising

  2. On Tue, 20 Apr 2004 15:17:49 +0200, Patrick Vanhoof wrote:
    > 'Syntax error on token "(", "Identifier" expected'
    > Anyone knows why I get this message?
    >
    > public class Storage {
    > Storage st = new Storage();
    > String str = new String("dit is een string");
    > public static void main(String[] args) {
    > }
    > int storage(String s) {
    > return s.length() * 2;
    > }
    > System.out.println(st.storage(str));
    > }


    The last statement doesn't belong to any method (or constructor). Look
    at where your braces {} are.

    /gordon


    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 20, 2004
    #2
    1. Advertising

  3. Patrick Vanhoof

    Peter Kirk Guest

    "Patrick Vanhoof" <> skrev i en
    meddelelse news:408522f4$0$10897$...
    > Hi,
    >
    > I am pretty new to Java, and am therefor already blocked by a simple

    program
    > (code avalaible below). The last line of the program (the println) gives a
    > syntax error:
    > 'Syntax error on token "(", "Identifier" expected'
    > Anyone knows why I get this message?
    >
    > public class Storage {
    > Storage st = new Storage();
    > String str = new String("dit is een string");
    > public static void main(String[] args) {
    > }
    > int storage(String s) {
    > return s.length() * 2;
    > }
    > System.out.println(st.storage(str));
    > }


    One thing is your println is not in a method.
     
    Peter Kirk, Apr 20, 2004
    #3
  4. try this:

    public class Storage {

    public static void main(String[] args)
    {
    Storage st = new Storage();
    String str = new String("dit is een string");
    System.out.println(st.storage(str));
    }
    int storage(String s)
    {
    return s.length() * 2;
    }

    }

    HTH,

    Jordan.

    "Patrick Vanhoof" <> wrote in message
    news:408522f4$0$10897$...
    > Hi,
    >
    > I am pretty new to Java, and am therefor already blocked by a simple

    program
    > (code avalaible below). The last line of the program (the println) gives a
    > syntax error:
    > 'Syntax error on token "(", "Identifier" expected'
    > Anyone knows why I get this message?
    >
    > public class Storage {
    > Storage st = new Storage();
    > String str = new String("dit is een string");
    > public static void main(String[] args) {
    > }
    > int storage(String s) {
    > return s.length() * 2;
    > }
    > System.out.println(st.storage(str));
    > }
    >
    >
    > Thanks in advance,
    > Patrick
    >
    >
     
    Jordan Humber, Apr 20, 2004
    #4
  5. Thanks, but how do I put it in a method? And how call it then?


    "Peter Kirk" <peter> schreef in bericht news:40852443$...
    >
    > "Patrick Vanhoof" <> skrev i en
    > meddelelse news:408522f4$0$10897$...
    > > Hi,
    > >
    > > I am pretty new to Java, and am therefor already blocked by a simple

    > program
    > > (code avalaible below). The last line of the program (the println)

    gives a
    > > syntax error:
    > > 'Syntax error on token "(", "Identifier" expected'
    > > Anyone knows why I get this message?
    > >
    > > public class Storage {
    > > Storage st = new Storage();
    > > String str = new String("dit is een string");
    > > public static void main(String[] args) {
    > > }
    > > int storage(String s) {
    > > return s.length() * 2;
    > > }
    > > System.out.println(st.storage(str));
    > > }

    >
    > One thing is your println is not in a method.
    >
    >
     
    Patrick Vanhoof, Apr 20, 2004
    #5
  6. Patrick Vanhoof wrote:
    > > > I am pretty new to Java, and am therefor already blocked by a simple

    > > program
    > > > (code avalaible below). The last line of the program (the println)

    > gives a
    > > > syntax error:
    > > > 'Syntax error on token "(", "Identifier" expected'
    > > > Anyone knows why I get this message?
    > > >
    > > > public class Storage {
    > > > Storage st = new Storage();
    > > > String str = new String("dit is een string");
    > > > public static void main(String[] args) {
    > > > }
    > > > int storage(String s) {
    > > > return s.length() * 2;
    > > > }
    > > > System.out.println(st.storage(str));
    > > > }

    > >
    > > One thing is your println is not in a method.
    > >

    > Thanks, but how do I put it in a method? And how call it then?
    >

    Please don't toppost.

    You put that code in a method the same way you've put return s.length() * 2
    inside a method.

    Also, please ask these kind of beginner questions in c.l.j.help.
    But please read the java tutorial first. These concepts are clearly
    explained there:

    http://java.sun.com/docs/books/tutorial/java/index.html

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Apr 20, 2004
    #6
  7. Patrick Vanhoof

    Roedy Green Guest

    Roedy Green, Apr 21, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Balaji
    Replies:
    3
    Views:
    10,170
  2. gabriele renzi
    Replies:
    2
    Views:
    230
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    230
  4. Good Night Moon
    Replies:
    9
    Views:
    305
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    335
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page