0

I have a jsp page through which I select A value from drop down which has data time format and I have a calender from where I pick data and time.When I submit this form then another JSP Page is called which calls a method defined in java class. The method has following code

public  LinkedHashMap<Double, String> ClosestToMultiplesOfTen_User(String start,String end) throws SQLException {

int row_id ;
int bIdx = 0;
double[] vals=null;
int rowIndex = 0 ;
int i=0;

try
        { 
          con = getConnection();
          stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);     
   String sql="select distinct beam_current from INDUS2_BDS.dbo.DCCT where logtime between '"+start+"' and '"+end+"'"+
                  "and (beam_current like '%9.95' or beam_current like '%9.96' or beam_current like '%9.97' or beam_current like '%9.98' or  beam_current like '%9.99'  or beam_current like '%0' or beam_current like '%_0.01' or beam_current like '%_0.02' or beam_current like '%_0.03' or beam_current like '%_0.04' or beam_current like '%_0.05' or beam_current like '%_0.06') ";

         stmt.executeQuery(sql);
          rs = stmt.getResultSet();

         rs.last();
            int row_cnt=rs.getRow();
            System.out.println("row_count of closest " +row_cnt);
             vals = new double[row_cnt];
            rs.beforeFirst();
while(rs.next()) 
    {
   for(int j=0; j<1; j++)
         {
           vals[i]  = rs.getDouble(1);
         }
        i++;
     }
    }
 catch( Exception e )
    {
        System.out.println("\nException "+e);
    }


//  get the max value, and its multiple of ten to get the number of buckets
 double max = java.lang.Double.MIN_VALUE;
 for (double v : vals) max = Math.max(max, v);
Arrays.sort(vals);
System.out.println("value at vals[0] c "+vals[0]);
double min=vals[0];
int m2=(int) Math.round(min);
int m3=(int) Math.round(max);

int bucketCount = 1+((m3-m2)/10);
double[] bucket =new double[bucketCount];

 //  initialise the buckets array to store the closest values
double[][] buckets = new double[bucketCount][3];
for (int i1 = 0; i1 < bucketCount; i1++){
     // store the current smallest delta in the first element
     buckets[i1][0] = java.lang.Double.MAX_VALUE;
     // store the current "closest" index in the second element
     buckets[i1][1] = -1d;
     // store the current "closest" value in the third element
     buckets[i1][2] = java.lang.Double.MAX_VALUE;
 }

 //  iterate the rows
 for (row_id=1 ; row_id < vals.length; row_id++)
 {
     //  get the value from the row
     double v = vals[row_id];
     //  get the closest multiple of ten to v
     double mult = getMultipleOfTen(v);
     //  get the absolute distance of v from the multiple of ten
     double delta = Math.abs(mult - v);
     //  get the bucket index based on the value of `mult`
    bIdx = (int)(mult / 10d);
   // System.out.println("value of bidx for bucket index is"+bIdx);
     //    test the last known "smallest delta" for this bucket
     if (buckets[bIdx][0] > delta)
     {
      //  this is closer than the last known "smallest delta"
       buckets[bIdx][0] = delta;
       buckets[bIdx][1] = row_id;
       buckets[bIdx][2] = v;

     }
  }  
//   print out the result
for (int i1 =1; i1 < buckets.length; i1++)
{
      bucket = buckets[i1];
     rowIndex = (int) bucket[1];
 double rowValue = bucket[2];
      DecimalFormat twoDForm = new DecimalFormat("#.##"); 
      System.out.println("row index closeset "+rowIndex+ "value is closest "+rowValue);
   rs.absolute(rowIndex+1);
      user_current_map.put(java.lang.Double.valueOf(twoDForm.format(rs.getDouble(1))),"");

     }
System.out.println("user_current_map "+user_current_map);

return user_current_map;
}

 public  double getMultipleOfTen(double v)
            {
                 System.out.println(10d * Math.round(v / 10d));
                return 10d * Math.round(v / 10d);
            }

Value of bucketcount retrieved is correct. But I'm getting exception at line if (buckets[bIdx][0] > delta) of java.lang.ArrayIndexOutOfBoundsException:21. 21 is the value retrieved from bucket count.I'm not getting my mistake,where I'm going wrong??

2 Answers 2

1

I got it, the value of **bIdx = (int)(mult / 10d);** starts from m2/10 value rather than starting from 0,so when it tries to access the index which is greater than the size of buckets which is equal to bucket count,array index out of bound exception occurs.

Solution= bIdx = (int)(mult / 10d) - m2/10;

Divide it by minimum value retrieved /10 ,so that it will start from 0 and goes till buckets size.

Sign up to request clarification or add additional context in comments.

Comments

0

Array index starts from 0 and goes till n-1. In your case, you could access elements from index 0 and going till 21-1=20. So you should use:

 if (buckets[bIdx-1][0] > delta)

5 Comments

,I did change if (buckets[bIdx-1][0] > delta),but still its showing java.lang.ArrayIndexOutOfBoundsException: at if (buckets[bIdx-1][0] > delta)
What was the exception this time? are you sure bIdx doesnt go beyond 21?
same,that index out of bound,I have edited my cod e,see I have added the function getMultipleOfTen(double v). As bldx holds value from mult/10d and mult holds value from getMultipleOfTen(v);
When I printed the value of v from getMultipleOfTen,then its going till 160, but it has values till 200.Can't understand that y it is being stopped at 160 .Its starts from 50 which is correct and it should go to 200.In bucket count,m2 is 50 and m3 is 200.Can u please tell me my mistake..
When I print the values of mult and v from for (row_id=1 ; row_id < vals.length; row_id++) then the values are going upto 160,but y can't understand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.