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

Thread: Problem with condition

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Problem with condition

    Hi, I'm having problem with this code.

    import java.io.*;
    public class ConditionalStatement {
        public static void main(String[]args) {
     
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
     
            String num;
     
            try {
                num = input.readLine();
                int num1 = Integer.parseInt(num);
     
                int x=(num1>=0)? "positive":"negative";
                System.out.println("The number entered is "+x+".");
            }
     
            catch (IOException e) {
                System.out.println("Error!");
            }
     
            }
    }

    How come this line says "incompatible types"?
    int x=(num1>=0)? "positive":"negative";

    I just declared x as integer. What's wrong with that?


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Problem with condition

    you are using ternary statement(operation), so i guess its a bit confusing.. ,,well the problem is this -- if num1 is greater than or equal to '0' then "ASSIGN" this String value to the variable 'x' which is an integer...


    change the variable 'x' to String
    Last edited by chronoz13; December 7th, 2009 at 05:21 AM.

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

    shamed (December 7th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem with condition

    In the code you have.

    int x=(num1>=0)? "positive":"negative";

    You are trying to check the value of num1 and return a string and assign it to int x. Change the int x to be String x.

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    shamed (December 7th, 2009)

  6. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Problem with condition

    Thanks for answering, and thanks for the instruction. Now the problem is, it does not output the "negative" even if I input -1 or any negative number. It always displays "positive".

  7. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem with condition

    Rewrite it to be this.

    String x=(num1 >= 0 ? "positive" : "negative");

    I think your parenthesis was in the wrong place.

    // Json

  8. The Following User Says Thank You to Json For This Useful Post:

    shamed (December 7th, 2009)

  9. #6
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with condition

    Quote Originally Posted by Json View Post
    Rewrite it to be this.

    String x=(num1 >= 0 ? "positive" : "negative");

    I think your parenthesis was in the wrong place.

    // Json
    Thanks for answering, and thanks for the instruction. Now the problem is, it does not output the "negative" even if I input -1 or any negative number. It always displays "positive".

  10. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Problem with condition

    This code works fine for me.

            int num1 = -1;
            String x = (num1 >= 0 ? "positive" : "negative");
            System.out.println(x);

    // Json

  11. #8
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Problem with condition

    Quote Originally Posted by Json View Post
    Rewrite it to be this.

    String x=(num1 >= 0 ? "positive" : "negative");

    I think your parenthesis was in the wrong place.

    // Json
    This code works fine with me. Thank you Json.