padreati / rapaio-jupyter-kernel

Java jupyter kernel
MIT License
53 stars 7 forks source link

java.lang.NegativeArraySizeException While taking input #48

Closed ShashaankHari closed 8 months ago

ShashaankHari commented 8 months ago

I discovered the kernel recently and wanted to use it for quick code snippets but i am stuck with Scanner

Doesn't matter if it is an array or not, even

int numElements = inp.nextInt();

gives negative array exception and after certain retries

 java.lang.IndexOutOfBoundsException: end
import java.util.*;
Scanner inp= new Scanner(System.in);
    int arr[] = new int[5];
    int s=0;
    int avg =0;
    for (int x=0;x<arr.length;x++){
        arr[x]=inp.nextInt();
        System.out.println(arr[x]);
        s+=x;
    }
    avg=s/arr.length;
    System.out.println("Sum: "+ s +"\nAverage: "+avg);

same code works on ijava python kernel and regular java class

after each run the java.lang.NegativeArraySizeException gets incremented twice negatively

java.lang.NegativeArraySizeException: -2

   at org.rapaio.jupyter.kernel.core.java.io.DynamicFIFOByteBuffer.take(DynamicFIFOByteBuffer.java:30)
   at org.rapaio.jupyter.kernel.core.java.io.JShellInputStream.read(JShellInputStream.java:72)
   at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:329)
   at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:372)
   at java.base/sun.nio.cs.StreamDecoder.lockedRead(StreamDecoder.java:215)
   at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:169)
   at java.base/java.io.Reader.read(Reader.java:212)
   at java.base/java.io.InputStreamReader.read(InputStreamReader.java:168)
   at java.base/java.util.Scanner.readInput(Scanner.java:890)
   at java.base/java.util.Scanner.next(Scanner.java:1600)
   at java.base/java.util.Scanner.nextInt(Scanner.java:2267)
   at java.base/java.util.Scanner.nextInt(Scanner.java:2221)
   at .(#6:4)
|    [//](https://github.com/padreati/rapaio-jupyter-kernel/issues/new) arr={1,2,3,4,5};
|        [//](https://github.com/padreati/rapaio-jupyter-kernel/issues/new) i=0;
|        for (int x=0;x<arr.length;x++){
|-->         arr[x]=inp.nextInt();
|            s+=x;
|        }
padreati commented 8 months ago

Hi @ShashaankHari. Sorry for being late, I did not noticed your issue. It was indeed a mistake into the buffering used to collect user input. I have a fix for that and it will be available in the next release 1.4.0. Which I hope I will release in a matter of days. Thank you for letting me know.

padreati commented 8 months ago

Resolved