mamruoc
asked on
Compare long
Hi!
seems like I can't do:
long a= 9876543435351;
long b =1232134343344;
if (a > b) {
// do something
}
found that I had to use compareTo for long, but how?
regards
seems like I can't do:
long a= 9876543435351;
long b =1232134343344;
if (a > b) {
// do something
}
found that I had to use compareTo for long, but how?
regards
(Where 'a' is defined as Long). The alternative is
if (a.compareTo(b) > 0) {
}
if (a.compareTo(b) > 0) {
}
... and if they *are* defined as long, then your posted code will work
ASKER
Error Cannot invoke longValue() on the primitive type long
hm....
hm....
a > b is correct for all simple number-carrying variables
compareTo( ) and equals( ) are for reference variables, when comparing (part of) the object's contents.
;JOOP!
compareTo( ) and equals( ) are for reference variables, when comparing (part of) the object's contents.
;JOOP!
ASKER
I'm doing:
long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
if((dateObject.getTime() < time)) {
}
long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
if((dateObject.getTime() < time)) {
}
>>seems like I can't do:
>>long a= 9876543435351;
>>long b =1232134343344;
Right, you can't since both values are too large to fit in a long
>>long a= 9876543435351;
>>long b =1232134343344;
Right, you can't since both values are too large to fit in a long
>>I 'm doing:
>>long time = 0 ;
>>Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
>>if((dateObject.getTime() < time)) {
>>}
That's OK. But I think you mean
if((dateObject.getTime() > time))
>>long time = 0 ;
>>Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
>>if((dateObject.getTime()
>>}
That's OK. But I think you mean
if((dateObject.getTime() > time))
ASKER
I'm doing:
long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
if((dateObject.getTime() < time)) {
}
I'm trying to make a timeout function using linuxtimestamp....
time will be fill with timestamp and added, let's say 2000, for 2 sec timeout....
long time = 0 ;
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
if((dateObject.getTime() < time)) {
}
I'm trying to make a timeout function using linuxtimestamp....
time will be fill with timestamp and added, let's say 2000, for 2 sec timeout....
zzynx, you are confusing int and long: long can contain as much as 2^63
;JOOP!
;JOOP!
>>I'm doing:
That's fine
That's fine
Please post the code that's failing
>> Error Cannot invoke longValue() on the primitive type long
longValue() must be invoked on a Long (capital L) not on a long (small l)
longValue() must be invoked on a Long (capital L) not on a long (small l)
>> zzynx, you are confusing int and long: long can contain as much as 2^63
Did you try to compile
>>long a= 9876543435351;
>>long b =1232134343344;
It gives me
integer number too large: 9876543435351
integer number too large: 1232134343344
Did you try to compile
>>long a= 9876543435351;
>>long b =1232134343344;
It gives me
integer number too large: 9876543435351
integer number too large: 1232134343344
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I see: that should've been
long a= 9876543435351L;
long b =1232134343344L;
(remark the L at the end)
long a= 9876543435351L;
long b =1232134343344L;
(remark the L at the end)
ASKER
public void run() {
if (DEBUG) System.out.println("UDP AcceptThread accepting messages...");
try {
acceptSocket = new DatagramSocket(UDP_IN_PORT );
byte[] buffer = new byte[defaultBufferLength];
int counter=0;
while (true) {
incoming = new DatagramPacket(buffer, buffer.length);
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
try {
if(recording || !playing || acceptingNewStream) { //Save to file, if system busy playback or sending.
if(time == 0) { // timeout == 0 for initiate a new stream
in_baos = new ByteArrayOutputStream();
time = 1;
}
if((dateObject.getTime() < time) || (time == 1)) { //time < timeout || timeout == 1
acceptSocket.receive(incom ing); /* Accepts incoming udp stream */
try {
in_baos.write(incoming.get Data());
if(incoming.getLength() > 0) {
if(time == 1) {
System.out.print("Acceptin g new stream");
}
time = (dateObject.getTime() + (timeOut*1000));
acceptingNewStream = true;
System.out.print(".");
}
}
catch (IOException e) {
System.out.println( "Writing to stream: " + e.getMessage());
}
}
else {
System.out.println("Someth ing went wrong: ");
}
}
else { // System not busy
///TODO: Fix playing when not recording or playback.
}
}
catch (IOException e) {
System.err.println(e);
}
} // end while
} // end try
catch (SocketException se) {
System.err.println(se);
} // end catch
} // end run
I got a ticker thread running beside too, to timeout the whole system 'cause incoming = new DatagramPacket(buffer, buffer.length); seems to hang if there is no new incoming....
if (DEBUG) System.out.println("UDP AcceptThread accepting messages...");
try {
acceptSocket = new DatagramSocket(UDP_IN_PORT
byte[] buffer = new byte[defaultBufferLength];
int counter=0;
while (true) {
incoming = new DatagramPacket(buffer, buffer.length);
Date dateObject = new Date(); /* Create dateobject to be able to use linux timestamp */
try {
if(recording || !playing || acceptingNewStream) { //Save to file, if system busy playback or sending.
if(time == 0) { // timeout == 0 for initiate a new stream
in_baos = new ByteArrayOutputStream();
time = 1;
}
if((dateObject.getTime() < time) || (time == 1)) { //time < timeout || timeout == 1
acceptSocket.receive(incom
try {
in_baos.write(incoming.get
if(incoming.getLength() > 0) {
if(time == 1) {
System.out.print("Acceptin
}
time = (dateObject.getTime() + (timeOut*1000));
acceptingNewStream = true;
System.out.print(".");
}
}
catch (IOException e) {
System.out.println( "Writing to stream: " + e.getMessage());
}
}
else {
System.out.println("Someth
}
}
else { // System not busy
///TODO: Fix playing when not recording or playback.
}
}
catch (IOException e) {
System.err.println(e);
}
} // end while
} // end try
catch (SocketException se) {
System.err.println(se);
} // end catch
} // end run
I got a ticker thread running beside too, to timeout the whole system 'cause incoming = new DatagramPacket(buffer, buffer.length); seems to hang if there is no new incoming....
>>long a= 9876543435351;
should be
long a= 9876543435351L;
should be
long a= 9876543435351L;
Try:
long a = 9876543435351l;
long b = 1232134343344l;
if (a > b) {
// do something
}
(Notice the "l" at the end of the long numbers)
long a = 9876543435351l;
long b = 1232134343344l;
if (a > b) {
// do something
}
(Notice the "l" at the end of the long numbers)
Said that :°)
>>long a= 9876543435351;
>>long b =1232134343344;
>Right, you can't since both values are too large to fit in a long
Nope they are not. They can easily fit in a long.
>>long b =1232134343344;
>Right, you can't since both values are too large to fit in a long
Nope they are not. They can easily fit in a long.
Yes, I know.
cf. previous comment.
cf. previous comment.
As the title suggests, this thread is long.
Shouldn't this
time = (dateObject.getTime() + (timeOut*1000));
be
time = ((new Date()).getTime() + (timeOut*1000));
time = (dateObject.getTime() + (timeOut*1000));
be
time = ((new Date()).getTime() + (timeOut*1000));
ASKER
Neede acutally long,
but the suggestion about timout function was just what I needed.. :D
thanks alot everybody
but the suggestion about timout function was just what I needed.. :D
thanks alot everybody
Oh, no longer interested. OK.
ASKER
Hi,
no hard feelings.. It's still intresting....
But CEHJ gave me a solution I needed....
:S
no hard feelings.. It's still intresting....
But CEHJ gave me a solution I needed....
:S
8-)
>>no hard feelings
No offence meant.
No offence meant.
if (a.longValue() > b.longValue()) {
}