File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes I have a problem with local variable access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I have a problem with local variable access" Watch "I have a problem with local variable access" New topic

I have a problem with local variable access

Deepak Kumar Jha

Joined: Feb 07, 2013
Posts: 18

Question here is that you are given range of number (A to B) and you have to tell the sum of slope of all the number between A and B.

Now we define the term slope of a number N. Slope of a number N is the number of digits (excluding the first and the last digit) which is either maxima or minima in that number.

Consider a number N. A digit in number N is called minima if it is smaller than both of its neighbour and a digit is called maxima if it is larger than both of its neighbour.

In case of wrong ranges or unexpected ranges, please return output as -1

This is the problem my solution is

import java.util.*;
public class CandidateCode
public static void slope_sum(int input1,int input2)
int upperLimit=input1;
int lowerLimit=input2;
int count1=0;
ArrayList<Integer> list=new ArrayList<Integer>();
for(int i=lowerLimit;i<=upperLimit;i++)
int size=list.size();
for(int i=0;i<=size;i++)
int digit=0;
catch(Exception e)
ArrayList list1=new ArrayList();
int rem=digit%10;


public static void method1(ArrayList list){
int sizeList1=list.size();
ArrayList<Integer> list2=list;
int count=0;
for(int j=0;j<=sizeList1;j++)
int digitSplited1=(int) list2.get(j);
int digitSplited2=(int) list2.get(j+1);
int digitSplited3=(int) list2.get(j+1);
if((digitSplited1>digitSplited2 && digitSplited3>digitSplited2) || (digitSplited2<digitSplited1 && digitSplited2><digitSplited3))



public static void main(String args[])
System.out.println("Enter the Lower Limit");
Scanner in = new Scanner(;
int lower = in.nextInt();
System.out.println("Enter the Upper Limit");
int upper=in.nextInt();


but output i get is

Enter the Lower Limit
Enter the Upper Limit
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at abc.CandidateCode.method1(
at abc.CandidateCode.slope_sum(
at abc.CandidateCode.main(

Can anyone help me out from this confusion.>
Guillaume Drouet
Ranch Hand

Joined: Apr 16, 2012
Posts: 36

not sure this is the right place for this topic. However, i will try to help you.

The problem is inside of the method2.

You are iterating on the elements of list2. As you can see, in the for statement, the index range is between 0 and the value inside sizeList1 (3 according to the stacktrace). It means that the available index are 0, 1 and 2. The loop statement looks good, because the the loop will stop when the value of j will raise the value 2. However, when this value of j is "two", you are trying to get the value at the index 3 :

You need to check that j not equals to the last possible index (here the value 2) to execute this statement.
Deepak Kumar Jha

Joined: Feb 07, 2013
Posts: 18

I finish this problem you are right there was a problem with array index.

I agree. Here's the link:
subject: I have a problem with local variable access
It's not a secret anymore!