while ( x >= 0 )
{
System.out.println("Enter next value");
x = arr.nextInt();
if(x >= 0) //Add this if block
{
a[size]=x;
size++;
}
}
Additionally, change this line:
double[] a=new double[10];
to
int[] a = new int[10];
Finally, when you create a primitive int
(or double
), even in an array, the default value is 0
. So if you enter -999
as your first input, your array will look like this:
{0,0,0,0,0,0,0,0,0,0}