HyperBlue - 1 year ago 104
Java Question

# How to Check Whether a Variable is an Integer or Not

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.

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]);
``````

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download