-2

The objective is trying to create a code that gets the difference of the max and min number of the Array List.

As a beginner I am having trouble understanding why I get that maxRange is not declared in my code.

private int firstElement; 
public int maxRange(ArrayList<Integer> arr)
{
  if (maxRange.size() ==0)
  {
      return 0; 
  }
  if (maxRange.size()==1)
  {
      return 1;

  }
  int FirstElement = maxRange.get(1);
  int max = firstElement;
  int min = firstElement;

  for ( int i =0; i < maxRange.size(); i++) 
  {
      int elementValue = maxRange.get(i);
      if(max < elementValue)
      {
          max = elementValue;
      }
      if (elementValue < min)
      {
          min = elementValue;
      }
  }
  return (max -min) + 1;
}
public class Scratchpad
{
   public static void main(String[] args)
   {
       List <Integer> maxRange = new ArrayList <>();

       maxRange.add(3);
       maxRange.add(11);
       maxRange.add(25);
       maxRange.add(48);

       System.out.println(maxRange);
   }
}


Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • 1
    Do you understand the difference between a method and a variable, you have named them the same so this might confuse you. Also when you write a method that has a parameter then you use the parameter name in the code inside the method and not the name of the variable you call it with. You really need to read up on how to write and call a method in java. – Joakim Danielson May 02 '20 at 07:57

1 Answers1

0

If the code listing is all in one file then the problem is that firstElement and maxRange are declared outside of the Scratchpad class. They should be inside it.

fram
  • 1,551
  • 1
  • 7
  • 7