Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   Why does this happen? (https://www.askmehelpdesk.com/showthread.php?t=140596)

  • Oct 14, 2007, 12:20 PM
    albear
    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");
    }
    }
    }
  • Oct 14, 2007, 12:42 PM
    albear
    Any ideas...
  • Oct 14, 2007, 12:51 PM
    retsoksirhc
    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.
  • Oct 14, 2007, 12:58 PM
    albear
    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
  • Oct 14, 2007, 01:11 PM
    retsoksirhc
    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");
    }

  • Oct 14, 2007, 01:15 PM
    albear
    Yes it works thank you mr koster
  • Oct 14, 2007, 01:20 PM
    retsoksirhc
    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.
  • Oct 14, 2007, 01:22 PM
    albear
    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,

  • All times are GMT -7. The time now is 05:44 PM.