I am trying to convert Strings to their integer equivalents for faster comparison using ByteBuffer (java.nio.ByteBuffer).
I got a very peculiar exception using ByteBuffer.
public class LargeCompare {
public static void main(String args[]){
byte[]b ="zzz".getBytes();
ByteBuffer bb = ByteBuffer.wrap(b);
bb.getInt();
}
}
The above code does not raise an exception for strings of length 4 but raises one for ones of length 3 and less.
Can anyone help me in fixing this?
An
intis 32 bits, or 4 bytes, wide. You are a trying to read anintfrom a buffer that’s shorter than this. This is why you’re getting the exception.I don’t really follow where you’re going with this, so will refrain from making suggestions.