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: Not sure what's up with this....

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not sure what's up with this....[FIXED]

    So part of my homework assignment for Java is copying a tutorial out of the book (as well as making one up ourselves), well when I copied this one out of the book, it gave me an error on the "else" statement in Eclipse telling me to just "delete this token", clearly I don't want to do that. I'm not even quite sure why I would get such an error, as far as I'm aware it's a valid statement.

     
    import javax.swing.*;
    public class RepairName 
    {//start class RepairName
    	public static void main(String[] args)
    	{//start main method
    		String name, saveOriginalName;
    		int stringLength;
    		int i;
    		char c;
    		name = JOptionPane.showInputDialog(null, "Please enter your first and last name");
    		saveOriginalName = name;
    		stringLength = name.length();
    		for(i=0; i>stringLength; i++)
    		{//start for loop
    			c = name.charAt(i);
    			if(i==0);
    			{//start if loop
    				c = Character.toUpperCase(c);
    				name = c + name.substring(1,stringLength);
    			}//end if loop
    			else
    				if(name.charAt(i) == ' ')
    				{//start if loop
    					++i;
    					c = name.charAt(i);
    					c = Character.toUpperCase(c);
    					name = name.substring(0, i) + c +
    							name.substring(i + 1, stringLength);
    				}//end if loop
    		}//end for loop
    		JOptionPane.showMessageDialog(null, "Original name was " + saveOriginalName + "\nRepaired name is " + name);
     
    	}//end main method
     
     
    }//end class RepairName
    Anyway the entire error reads "Syntax error on token 'else'; delete this token".
    Any help would be nice, and thank you in advance.
    Last edited by Sylis; October 16th, 2012 at 06:18 AM. Reason: fixed


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Not sure what's up with this....

    An else is only valid when it follows an if in the syntax.
    Somehow there is no if statement preceding the else keyword.

    This is one example of how one error in the syntax can lead the compiler to throwing an error notice somewhere else.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what's up with this....

    I understand, adding the semi-colon to the line ended the statement right there.
    Even though there were no errors in the brackets, had there not been an error in the else statement it would have no run properly do to the if statement not having a procedure.
    The way it was written the if statement is pointless, sure it checks if I = 0 but it never does anything if it is 0.

    Thank you for your help.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Not sure what's up with this....

    Quote Originally Posted by Sylis View Post
    Thank you for your help.
    You are welcome.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Not sure what's up with this....

    :headdesk:

    There is no such thing as an if loop!
    Improving the world one idiot at a time!