Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# I have a problem with local variable access

Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
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.io.*;
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();
System.out.println(size);
for(int i=0;i<=size;i++)
{
int digit=0;
try{
digit=list.get(i);
}
catch(Exception e)
{
System.out.println(e);
}
ArrayList list1=new ArrayList();
while(digit>0)
{
int rem=digit%10;
digit=digit/10;

}
method1(list1);
}

}
public static void method1(ArrayList list){
int sizeList1=list.size();
System.out.println(sizeList1);
ArrayList<Integer> list2=list;
int count=0;
if(sizeList1>2)
{
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))
{

count++;

}
}
}
System.out.println(count);

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

}
}

but output i get is

Enter the Lower Limit
100
Enter the Upper Limit
150
51
3
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(CandidateCode.java:52)
at abc.CandidateCode.slope_sum(CandidateCode.java:37)
at abc.CandidateCode.main(CandidateCode.java:70)

Can anyone help me out from this confusion.>

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
Hi,

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
Greenhorn
Posts: 18
• Number of slices to send:
Optional 'thank-you' note:
I finish this problem you are right there was a problem with array index.

 Get off me! Here, read this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser