This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I have a problem with local variable access

 
Deepak Kumar Jha
Greenhorn
Posts: 18
Java jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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++)
{
list.add(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;
list1.add(rem);

}
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.>
 
Guillaume Drouet
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java jQuery Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finish this problem you are right there was a problem with array index.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic