1

I'm new to programming in Java and I'm attempting to learn the ropes as I go along. I have ran into a problem, the problem stems with my inability to check whether a variable is an integer or not.

int[] values = new int[3];
private Random rand = new Random();
num1 = rand.nextInt(70);
num2 = rand.nextInt(70);
num3 = rand.nextInt(70);

values[1] = num1 + num2

//we are dividing values[1] by num3 in order to produce an integer solution. For example (4 + 3) / 2 will render an integer answer, but (4 + 3) / 15 will not. 
//The (correct) answer is values[2]

values[2] = values[1]/num3

if (num3 > values[1]) {
     //This is done to ensure that we are able to divide by the number to get a solution (although this would break if values[1] was 1).
     num3 = 2
     while (values[2] does not contain an integer value) {
         increase num3 by 1
        work out new value of values[2]
        }    
} else {
    while (values[2] does not contain an integer value) {
         increase num3 by 1
        work out new value of values[2] <--- I'm not sure if this step is necessary
    }
}

System.out.println(values[2]);
HyperBlue
  • 61
  • 1
  • 7

6 Answers6

2

Another way:

if(value%1 == 0){
  //True if Integer
}

E.g. 2.34f % 1 will be 0.34 but 2f % 1 is 0.0

Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75
2

The following will never happen because the values array is of type primitive int. As such checking if values[2] is an int is after the fact. values[] will always contain an int and trying to add an element to the values array that's not of type int will result in an ArrayStoreException being thrown.

If the type of the value being assigned is not assignment-compatible (§5.2) with the component type, an ArrayStoreException is thrown.

// All three are of type int
num1 = rand.nextInt(70);
num2 = rand.nextInt(70);
num3 = rand.nextInt(70);

// Not possible... 
while (values[2] does not contain an integer value) {

When you initialize the values array it will automatically have default the values of 0.

int[] values = new int[3]
System.out.println( Arrays.toString(values) );
> [0,0,0]
Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41
1

If you divide an integer by another integer you will always have an integer.

You need the modulo (remainder) operation

int num1 = 15;
int num2 = 16;
int num3 = 5;

System.out.println("num1/num3 = "+num1/num3);
System.out.println("num2/num3 = "+num2/num3);
System.out.println("num1%num3 = "+num1%num3);
System.out.println("num2%num3 = "+num2%num3);

If the modulo is not 0 then the result would not be an integer.

mkj
  • 2,761
  • 5
  • 24
  • 28
Peter
  • 5,728
  • 20
  • 23
1

If input value can be in numeric form other than integer , check by

if (x == (int)x){
   // Number is integer
}

If string value is being passed , use Integer.parseInt(string_var).

Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
sandipon
  • 986
  • 1
  • 6
  • 19
0
if(value == (int) value)

or a long (64-bit integer)

if(value == (long) value)

or can be safely represented by a float without a loss of precision

if(value == (float) value)

If a string value is being passed, use Integer.parseInt(value); which will return an integer if the input passed is the correct integer.

mkj
  • 2,761
  • 5
  • 24
  • 28
Devi Kiran
  • 598
  • 1
  • 5
  • 17
0

You can use instanceof operator to check if the given object is an Integer.

if(num instanceof Integer)
    System.out.println("Number is an integer");
Kedarnath
  • 260
  • 1
  • 3
  • 13