Ask Experts Questions for FREE Help !
Ask
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #1

    Oct 14, 2007, 12:20 PM
    why does this happen?
    when I input a number that is less than -5 it prints off 'this number is valid' and 'this number is invalid'
    I know its because of where its placed but how can I prevent it so that it only brings up one message.

    import java.util.*; // the package containing Scanner
    public class Ranges
    {
    public static void main(String[] args)
    {

    Scanner kybd = new Scanner(System.in);

    //input

    System.out.println("Enter an integer");
    int Num1;
    Num1 = kybd.nextInt();

    //calculations and output

    if (Num1 < -5)
    {
    System.out.println("Number is valid");
    }

    if ( (Num1 >= 4)&&(Num1 <= 12))
    {
    System.out.println("Number is valid");
    }
    else
    {
    System.out.println("Number is invalid");
    }
    }
    }
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #2

    Oct 14, 2007, 12:42 PM
    Any ideas...
    retsoksirhc's Avatar
    retsoksirhc Posts: 912, Reputation: 71
    Senior Member
     
    #3

    Oct 14, 2007, 12:51 PM
    I don't work with java, but I can see the control flow of the program. You need to use an elseif instead of an if for the seconf condition.
    if ( (Num1 >= 4)&&(Num1 <= 12))
    it's got one if, but then instead of doing an ELSE if, it just goes to a second if, and then when it gets to the else, it goes through it because the integer didn't meet the requirements for the second if, even though it may have for the first.

    It usually helps to do indenting, so you can see exactly where one block of IF statements starts and stop, and the next starts.
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #4

    Oct 14, 2007, 12:58 PM
    I tried replacing my second 'if' with 'elseif', but it doesn't like that I tried putting brackets around both 'if' statements but it didn't like either of them (separetly), thanks
    retsoksirhc's Avatar
    retsoksirhc Posts: 912, Reputation: 71
    Senior Member
     
    #5

    Oct 14, 2007, 01:11 PM
    I just looked up the syntax, it's actually else if, not elseif (two words)
    Try this:
    Code:
    if (Num1 < -5)
    {
    System.out.println("Number is valid");
    }
    
    else if ( (Num1 >= 4)&&(Num1 <= 12))
    {
    System.out.println("Number is valid");
    }
    else
    {
    System.out.println("Number is invalid");
    }
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #6

    Oct 14, 2007, 01:15 PM
    Yes it works thank you mr koster
    retsoksirhc's Avatar
    retsoksirhc Posts: 912, Reputation: 71
    Senior Member
     
    #7

    Oct 14, 2007, 01:20 PM
    Quote Originally Posted by albear
    yes it works thankyou mr koster
    No problem. And might I say, that first quote in your signature is pretty awesome. The Gary Jules cover of the song is much better, in my opinion.
    albear's Avatar
    albear Posts: 1,594, Reputation: 222
    Ultra Member
     
    #8

    Oct 14, 2007, 01:22 PM
    Quote Originally Posted by retsoksirhc
    No problem. And might I say, that first quote in your signature is pretty awesome. The Gary Jules cover of the song is much better, in my opinion.
    Agreed it is rather cool,

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

What will happen next? [ 4 Answers ]

What is going to happen next if the court ordered my child's father and I to go to mediation, and it was canceled because he didn't pay his part of it.(It's concerning custody and child support)? Also he is really behind in child support and this is a plot to try to get out of paying it. Ha Ha

When will this happen for me [ 4 Answers ]

I would like to know will I ever have children and if so when:)

What can happen next! [ 58 Answers ]

If a 20 yr old guy fingers a 15 year old girl and she lies to the cops and said she had no problem with it, what would happen?? What can she say to NOT get him in trouble... and to possibly not get her parents to find out?:confused: :(

What will happen to me if. [ 2 Answers ]

I have a 14 year old who has not been to school in about 4 months.What trouble am I in?He is very stubborn,bad tempered and will not what so ever go to school. I cry every morning trying to get him up and understand that I am in very deep trouble.I tell him I could go to jail and he says I'll get...

What can I do and What can Happen? [ 1 Answers ]

I live in Massachusettes, I have never been in any kind of trouble my whole life. I am a 43 year old female. I have been on disability for 5 years do to many health problems, and I raise my child alone, father never pays support. Things have been stressful some days I am in bed for weeks...


View more questions Search