Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Illegal start of expression? // begginer

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Illegal start of expression? // begginer

    Good day

    I can't figure out why the complier is giving the following error message "illegal start of expression" once it reach the countSpaces method.
    The way I see it (and I am obviously seeing it wrong), the code is public since why not, int since it returns an int # and the method has return int in it, and static I am not too sure but I get the same error message whether or not it is included

    Any indications of why this is occuring would be appreciated!

    Thanks

    Chris

       public static void main (String args[]) {
     
            Scanner in = new Scanner(System.in);
            String sentence;
     
            System.out.println("type a sentence");
            sentence = in.nextLine();
             countSpaces (sentence);
     
            public static int countSpaces (String test) {
                int counter  = 0;
                for (int i=0 ; i < test.length() ; i++) {
                    if (test.charAt(i) ==(' ')) {
                        counter++;
                    }
                } 
                return counter;
            }
        }
    Last edited by Christian_Doucet; July 26th, 2014 at 09:50 AM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Illegal start of expression? // begginer

    The main is in itself a method, and you cannot have methods inside methods. You just need to reorganize your brackets:
    public static void main (String args[]) {
     
            Scanner in = new Scanner(System.in);
            String sentence;
     
            System.out.println("type a sentence");
            sentence = in.nextLine();
             countSpaces (sentence);
            }
            public static int countSpaces (String test) {
                int counter  = 0;
                for (int i=0 ; i < test.length() ; i++) {
                    if (test.charAt(i) ==(' ')) {
                        counter++;
                    }
                } 
                return counter;
            }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Christian_Doucet (July 26th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    My Mood
    Busy
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start of expression? // begginer

    Cool, thanks for the help!

Similar Threads

  1. Illegal start of expression
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 22
    Last Post: April 14th, 2013, 06:31 AM
  2. Illegal start of expression
    By bad_newbie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2013, 08:57 PM
  3. Illegal start of expression!
    By NoobOfTheMonth in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 11th, 2013, 09:53 PM
  4. Help With illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2013, 01:20 PM
  5. need help with illegal start of expression
    By inshal in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 9th, 2013, 11:25 AM