Hi guys I’m trying to solve Java Practice problems here. I’ve noticed that instead of using the Scanner object and taking inputs if I create a suppose readInt() func and take input the Runtime is way faster.
For instance here is my readInt() which I use to take integer inputs just like a Scanner does.
static int readInt() throws IOException{
int number = 0;
int neg = 1;
if(offset==bufferSize){
offset = 0;
bufferSize = in.read(buffer);
}
for(;buffer[offset]<0x30 || buffer[offset]=='-'; ++offset)
{
if (buffer[offset]=='-')
neg = -1;
if(offset==bufferSize-1){
offset=-1;
bufferSize = in.read(buffer);
}
}
for(;offset<bufferSize && buffer[offset]>0x2f;++offset){
number = number*0x0a+buffer[offset]-0x30;
if(offset==bufferSize-1){
offset = -1;
bufferSize = in.read(buffer);
}
}
++offset;
return number*neg;
}
Is there any similar mehtod for Double and String variables. Scanner seems to take a lot of execution time. Also why does the scanner take so much time??