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

Thread: Help with If- Else statement program please

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with If- Else statement program please

    //How can I assign those "if" statements so i can use >12 and <20?

    //How can i get rid of the inttestscore so that i isnt not a constant. I want the user to put a "AGE" and have the following assignment statement co-oordinate with the text?

    example:::

    what is your age? 21

    "Howdy, you're a mover and a shaker!");




    its a simple fix i just need a little guidance why my if statements arnt working.




    import javax.swing.*;
    class IfElseDemo {
    public static void main(String[] args) {




    JOptionPane.showMessageDialog(null, "The Old Man says:");

    //Read in the age value
    String enterNum = JOptionPane.showInputDialog(null, "What is your age?");

    //convert string to a number
    double num1 = Double.parseDouble(enterNum);





    int testscore = 93;




    if (testscore <= 12) {


    JOptionPane.showMessageDialog(null, "Howdy youngster!");




    }else if (testscore >12 and <20); {



    JOptionPane.showMessageDialog(null, "Howdy, you're on your way!!");








    }else if (testscore >=20 and <40); {
    JOptionPane.showMessageDialog(null, "Howdy, you're a mover and a shaker!");


    }else if (testscore >=40); {


    JOptionPane.showMessageDialog(null, "Howdy, old timer");





    }


    }

    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with If- Else statement program please

    Semicolon after the if

    Next time use code tags:
    BB Code List - Java Programming Forums - The Java Community

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with If- Else statement program please

    that gave me more errors

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with If- Else statement program please

    }else if (testscore >=40); {
    // this semicolon    ----^
    // should be removed
    If you've changed your code and still got errors, post both the new code and the new errors. remove the semicolons between if expressions and the '{'s in your original code. It's valid Java code, but it will work in a very different way from what you're expecting.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with If- Else statement program please

    I just wanna input a number value have have the output line up to the correct statement of age.



    import javax.swing.*;
    class lukey {
    public static void main(String[] args) {




    JOptionPane.showMessageDialog(null, "The Old Man says:");

    //Read in the age value
    String enterNum = JOptionPane.showInputDialog(null, "What is your age?");

    //convert string to a number
    double num1 = Double.parseDouble(enterNum);





    int testscore = 93;




    if (testscore <= 12) {


    JOptionPane.showMessageDialog(null, "Howdy youngster!");




    }else if (testscore >12 and <20) {



    JOptionPane.showMessageDialog(null, "Howdy, you're on your way!!");








    }else if (testscore >=20 and <40) {
    JOptionPane.showMessageDialog(null, "Howdy, you're a mover and a shaker!");


    }else if (testscore >=40) {


    JOptionPane.showMessageDialog(null, "Howdy, old timer");





    }


    }

    }

    Quote Originally Posted by Sean4u View Post
    }else if (testscore >=40); {
    // this semicolon    ----^
    // should be removed
    If you've changed your code and still got errors, post both the new code and the new errors. remove the semicolons between if expressions and the '{'s in your original code. It's valid Java code, but it will work in a very different way from what you're expecting.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with If- Else statement program please

    my errors.


    C:\Users\Andrea\Documents\lukey.java:34: error: ')' expected
    }else if (testscore >12 and <20) {
    ^
    C:\Users\Andrea\Documents\lukey.java:34: error: not a statement
    }else if (testscore >12 and <20) {
    ^
    C:\Users\Andrea\Documents\lukey.java:34: error: ';' expected
    }else if (testscore >12 and <20) {
    ^
    C:\Users\Andrea\Documents\lukey.java:47: error: 'else' without 'if'
    }else if (testscore >=20 and <40) {
    ^
    C:\Users\Andrea\Documents\lukey.java:47: error: ')' expected
    }else if (testscore >=20 and <40) {
    ^
    C:\Users\Andrea\Documents\lukey.java:47: error: not a statement
    }else if (testscore >=20 and <40) {
    ^
    C:\Users\Andrea\Documents\lukey.java:47: error: ';' expected
    }else if (testscore >=20 and <40) {
    ^
    C:\Users\Andrea\Documents\lukey.java:51: error: 'else' without 'if'
    }else if (testscore >=40) {
    ^
    8 errors

    Tool completed with exit code 1

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with If- Else statement program please

    Use code tags when you post code and copy-pasted console output (errors):
    BB Code List - Java Programming Forums - The Java Community

    Your if expression syntax is wrong.
    Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    You're expressing an inequality in your if expressions, so there's no need to specify both ends of the range-of-interest in every if expression. If a number is not less than twelve in the first if expression, it is certainly either greater than or equal to 12 - there's no need to test that again! If you're happier expressing both limits in every if expression, then you'll need to use the right syntax for the 'and' operator. Refer to the Java Tutorial link.

Similar Threads

  1. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 09:25 AM
  2. how to make program of Depreciation using if statement and loops
    By Pulsifier in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 17th, 2011, 12:23 AM
  3. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2010, 12:31 AM
  4. If Statement
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 26th, 2010, 01:57 PM
  5. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM