java - How to compare an Integer with a long? -


i trying check if long integer greater integer.max value it's not working. it's straight forward wondering if there problem comparing integer object long have done because otherwise don't know problem is. @ moment when nexttotal value exceeds integer.max, kicks negative numbers instead of printing error message.

public integer initialvalue=0;  int amount = integer.parseint(amountstr);         system.out.println("received client: " + amount);          long nexttotal=amount+initialvalue;             if((nexttotal>integer.max_value)||    (nexttotal<integer.min_value)){                 system.out.println("error: integer has not been added total exceeds maximum integer value!");                 out.flush();             }                else{                 initialvalue+=amount;                 out.println(initialvalue); //server response                 system.out.println("sending total sum of integers client:"+initialvalue);                 out.flush();                 }             } 

the problem have added 2 ints, haven't been promoted long yet, overflows before being converted long, , of course int can never greater integer.max_value. converted long upon assignment, after addition.

convert long before addition, cast.

long nexttotal = (long) amount + initialvalue; 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -