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

Thread: if....else statement

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face if....else statement

    hi ,everyone . I am again presenting a simple code in which the line system.out .println has some errors. The code is

    class ifelsetest
    {
    public static void main(string args[])
    {
    int number []={50,34,87,19};
    int even=0,odd=0;
    for(int i=0;i<number.length; i++)
    {
    if((number[i]%2)==0)
    {
    even+=1;
    }
    else
    {
    odd+=1;
    }
    }
    }
    System.out.println("Even numbers":+even +odd numbers: +odd);
    }
    }
    The error is identifier expected, illegal start of type etc.... too many errors only in the last line . Plz help me resolve it . Thanks


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: if....else statement

    In future, always highlight your code in tags, such as the ones found in my signature. For now, please edit your post to do so.

    Points to take note:
    • Java is case-sensitive, and String starts with a capital S.
    • Your println statement is outside of the main method body, it needs to be inside.
    • Make sure each opening curly bracket has a matching closing bracket.
    • What you're printing is illegal syntax, should be ("Even numbers:" +even + "odd numbers:" +odd);
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: if....else statement

    Quote Originally Posted by Appu14 View Post
    hi ,everyone . I am again presenting a simple code in which the line system.out .println has some errors. The code is

    class ifelsetest
    {
    public static void main(string args[])
    {
    int number []={50,34,87,19};
    int even=0,odd=0;
    for(int i=0;i<number.length; i++)
    {
    if((number[i]%2)==0)
    {
    even+=1;
    }
    else
    {
    odd+=1;
    }
    }
    }
    System.out.println("Even numbers":+even +odd numbers: +odd);
    }
    }
    The error is identifier expected, illegal start of type etc.... too many errors only in the last line . Plz help me resolve it . Thanks
    But why aren't you using an IDE like eclipse to avoid these kind of issues??
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseño Web, Desarrollo de Software, SEO,
    LinkedIn

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: if....else statement

    @psabbate IDEs are not the the best thing for beginners. They hide too many aspects of what happens that a programmer needs to know.

    has some errors
    Please post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: if....else statement

    Quote Originally Posted by Norm View Post
    @psabbate IDEs are not the the best thing for beginners. They hide too many aspects of what happens that a programmer needs to know.


    Please post the full text of the error message.
    I'm sorry, but I have to disagree with you. Eclipse itself could answer a lot of questions that I 've seen posted here. If the beginners want to "think" like a compiler, they should study for SCJP.
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseño Web, Desarrollo de Software, SEO,
    LinkedIn

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: if....else statement

    Not many helpers on these forums would agree with you. IDEs are for the more experienced.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2012
    Location
    Missouri
    Posts
    12
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: if....else statement

    IDE's are good for beginners.

    Also, Error on the last line, check to see how many }'s you have.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: if....else statement

    Not before they learn about: classpath, packages, folders and the current directory.
    The IDEs hide all that so when something goes wrong, they have no idea what happened.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: if....else statement

    [QUOTE=Appu14;74339]hi . The code is

    class Ifelsetest
    {
    public static void main(string args[])
    {
    int number []={50,34,87,19};
    int even=0,odd=0;
    for(int i=0;i<number.length; i++)
    {
    if((number[i]%2)==0)
    {
    even+=1;
    }
    else
    {
    odd+=1;
    }
    }
    System.out.println("Even numbers":+even +"odd numbers:" +odd);
    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: if....else statement

    Are there still errors? Please post the full text of the error messages.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with if statement.
    By dreamer in forum Loops & Control Statements
    Replies: 2
    Last Post: May 30th, 2012, 02:27 PM
  2. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM
  3. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 12:57 PM
  4. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  5. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM

Tags for this Thread