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 5 of 5

Thread: illegal start of expression error!!!!

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default illegal start of expression error!!!!

    Hey...this is my code to reverse each word of the string input!!
    At line 12 it says 'illegal start of expression'....any idea how to troubleshoot this ??

    //Reverse each word of the sentence
    import java.io.*;
    class rev_word
    {
    	public static void main (String args[ ])throws IOException
    	{
    		BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    		String S = br.readLine();
    		S=S+"";int k=0; String word="";
    		for (int i=0; i<S.length();i++)
    		{
    			if (S.charAt(i) == ' ')) // <-- error shown at this line 'illegal start of expression'
    			{
    				word=S.substring (k, i);
    				k= i+1;
    				for (int j=word.length();j>=0;j++)
    				System.out.print (word.charAt(j));
    				System.out.print (" ");
    			}
    		}
    	}
    }
    Last edited by aks.1393; January 13th, 2011 at 01:23 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: illegal start of expression error!!!!

    Check the number of parenthesis: see Common java mistakes

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

    aks.1393 (January 14th, 2011)

  4. #3

    Default Re: illegal start of expression error!!!!

    Extra bracket in if condition. It should be:
    if(S.charAt(i)==' ')

  5. The Following 2 Users Say Thank You to worldblackstar For This Useful Post:

    aks.1393 (January 14th, 2011), javapenguin (January 14th, 2011)

  6. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: illegal start of expression error!!!!

    ok i removed the extra paranthesis
    and now the code luks somewhat like
    //Reverse each word of the sentence
    import java.io.*;
    class rev_word
    {
        public static void main (String args[ ])throws IOException
        {
            BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
            System.out.println("Enter a String");
            String S = br.readLine();
            S=S+"";int k=0; String word="";
            for (int i=0; i<S.length();i++)
            {
                if (S.charAt(i) == ' ')
                {
                    word=S.substring (k, i);
                    k= i+1;
                    for (int j=word.length();j>=0;j++)
                    System.out.print (word.charAt(j));
                    System.out.print (" ");
                }
            }
        }
    }

    it has compiled but now its giving a runtime error as follows:
    "Enter a String
    I love playing guitar
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:686)
    at rev_word.main(abc.java:18)"

  7. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: illegal start of expression error!!!!

    I believe this issue is caused by problems here:

                    for (int j = word.length(); j >= 0; j++)
                    System.out.print(word.charAt(j));
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  2. Illegal start of type?
    By mjpam in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 9th, 2010, 03:47 PM
  3. Illegal start
    By hing09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 25th, 2010, 05:23 PM
  4. "illegal modifier for parameter sum; only final is permitted
    By etidd in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2010, 07:51 AM
  5. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2009, 12:57 AM