I am trying to check if a long integer is greater than the INTEGER.MAX value but it's not working. It's very straight forward so I am just wondering if there is some problem with comparing an Integer object with a long as I have done because otherwise I don't know what the problem is. At the moment when the nextTotal value exceeds INTEGER.MAX, it kicks into negative numbers instead of printing the error message.
public Integer initialValue=0;
int amount = Integer.parseInt(amountStr);
System.out.println("Received from client: " + amount);
long nextTotal=amount+initialValue;
if((nextTotal>Integer.MAX_VALUE)|| (nextTotal<Integer.MIN_VALUE)){
System.out.println("Error: Integer has not been added as the total exceeds the Maximum Integer value!");
out.flush();
}
else{
initialValue+=amount;
out.println(initialValue); //server response
System.out.println("Sending total sum of integers to the client:"+initialValue);
out.flush();
}
}