31

Is there any in-built method in Java where you can find the user input's type whether it is positive, or negative and so on? The code below doesn't work. I am trying to find a way to input any in-built method that can do it at the if statement.

import java.util.Scanner;

public class Compare {

    public static void main(String[] args) { 

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int number = input.nextInt();

        if(number == int) 
            System.out.println("Number is natural and positive.");
    }
}
kar
  • 4,791
  • 12
  • 49
  • 74

6 Answers6

46

If you really have to avoid operators then use Math.signum()

Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.

EDIT : As per the comments, this works for only double and float values. For integer values you can use the method:

Integer.signum(int i)

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
31

What about using the following:

int number = input.nextInt();
if (number < 0) {
    // negative
} else {
   // it's a positive
}
blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • 1
    The second `if` is redundant... Simply use `else { /*positive*/ }` if you need the context block; or do away with the `else` and it's context block entirely if you don't need it: `if (number < 0) { /*negative*/ } /*positive*/` – CosmicGiant Sep 19 '16 at 19:27
  • 2
    0 is not positive or negative. http://mathforum.org/library/drmath/view/58735.html – devdanke Feb 09 '17 at 01:06
  • @devdanke Yet Java has [signed zeroes](http://stackoverflow.com/a/14771363/2908724). – bishop Feb 12 '17 at 03:28
  • 1
    @bishop Wow. Nice find! – devdanke Mar 04 '17 at 04:25
7

(You should you as Else-If statement to check the for the three different state (positive, negative, 0)

Here is a simple example (excludes the possibility of non-integer values)

  import java.util.Scanner;

  public class Compare {

   public static void main(String[] args) { 

    Scanner input = new Scanner(System.in);

    System.out.print("Enter a number: ");
    int number = input.nextInt();

    if( number == 0)
    { System.out.println("Number is equal to zero"); }
    else if (number > 0)
    { System.out.println("Number is positive"); }
    else 
    { System.out.println("Number is negative"); }


  }
 }
Neha Shukla
  • 3,572
  • 5
  • 38
  • 69
  • incorrect output for two cases; this line `int number = input.nextInt();` cause the wrong output for inputs of -1 and 0 – stoi2m1 Jan 02 '17 at 20:55
3

For integers you can use Integer.signum()

Returns the signum function of the specified int value. (The return value is -1 if the specified value is negative; 0 if the specified value is zero; and 1 if the specified value is positive.)

Kefirchiks
  • 280
  • 4
  • 13
0

Use like below code.

if(number >=0 ) {
            System.out.println("Number is natural and positive.");
}
STT LCU
  • 4,348
  • 4
  • 29
  • 47
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
-1

You could use if(number >= 0). The fact that you use int number = input.nextInt(); makes sure that it has to be an Integer.

Theolodis
  • 4,977
  • 3
  • 34
  • 53