Bear and Candies Game I am trying to improving my Java skills and I've been doing various different projects/challenges which I usually am able to complete. I unfortunately never have my code reviewed so while my code may work, I would love to hear ways to do things better. I went on Codechef and solved one of the problems and added some basic error handling in addition to what the problem asked. I tried to write the code as clearly as possible to avoid unnecessary comments.

Main.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

static List&lt;Integer&gt; maxEatingCaps_Limark = new ArrayList&lt;&gt;();
static List&lt;Integer&gt; maxEatingCaps_Bob = new ArrayList&lt;&gt;();

public static void main(String[] args) {

    setUp();
    Bear Limak = new Bear(maxEatingCaps_Limark,"Limak");
    Bear Bob = new Bear(maxEatingCaps_Bob,"Bob");
    Bear.startEating(Limak,Bob);

    }


public static void setUp() {
    int  numOfTestCases = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of test cases");

    try {
        numOfTestCases = Integer.parseInt(input.nextLine().trim());
    }
    catch (NumberFormatException e) {
        System.err.println("You must enter an number" );
        System.exit(1);
    }

    boolean validInput = false;
    for (int i = 0; i &lt; numOfTestCases ; i++) {
        validInput = false;

        while (!validInput)
            try {

                String maxEatingInput = input.nextLine();
                String[] individualEatingCaps = maxEatingInput.trim().split("\\s+");
                maxEatingCaps_Limark.add(Integer.parseInt(individualEatingCaps[0]));
                maxEatingCaps_Bob.add(Integer.parseInt(individualEatingCaps[1]));
                validInput = true;

            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                System.err.println("Enter in the form: INTEGER:(Limark's Max Eating Capacity) INTEGER:(Bob's Max Eating Capacity)");
                validInput = false;

            }

    }

}


Bear.java

import java.util.List;


public class Bear {

    private static int amountOfCandyToEat;
    private static boolean haveWinner;
    private static String currentLeader;
    private int candyConsumed;
    private List&lt;Integer&gt; maxEatingCapacity;
    private String name;
    private static int numOfTest;



    Bear(List&lt;Integer&gt; maxEatingCapacity, String name) {

        this.maxEatingCapacity = maxEatingCapacity;
        this.name = name;
        candyConsumed = 0;
        numOfTest = maxEatingCapacity.size();
    }

  public static void  startEating(Bear bear1, Bear bear2) {

        for (int i = 0; i &lt; numOfTest ; i++) {

            amountOfCandyToEat = 1;
            haveWinner = false;
            currentLeader = null;
            bear1.reset();
            bear2.reset();

            while(!haveWinner) {

                tryEating(bear1,i);
                tryEating(bear2,i);
            }
            System.out.println(currentLeader);

        }
        }
    private void reset() {
        candyConsumed = 0;
    }

    private static void tryEating(Bear bear, int index) {

        if (bear.getCandyConsumed() + Bear.getAmountOfCandyToEat() &lt;= bear.getMaxEatingCapacity(index)) {
            bear.setCandyConsumed(bear.getCandyConsumed() + Bear.getAmountOfCandyToEat());
            Bear.setCurrentLeader(bear.getName());
            amountOfCandyToEat++;
        }
        else{
            haveWinner = true;
        }
    }


  private String getName() {
      return name;
  }

  private static int getAmountOfCandyToEat() {
      return amountOfCandyToEat;
  }

  private int getMaxEatingCapacity(int index) {
      return maxEatingCapacity.get(index);
  }

  private int getCandyConsumed() {
      return candyConsumed;
  }

  private void setCandyConsumed(int candyConsumed) {
      this.candyConsumed = candyConsumed;
  }

  private static void setCurrentLeader(String currentLeader) {
      Bear.currentLeader = currentLeader;
  }
}
Helper methods


    int  numOfTestCases = 0;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of test cases");

    try {
        numOfTestCases = Integer.parseInt(input.nextLine().trim());
    }
    catch (NumberFormatException e) {
        System.err.println("You must enter an number" );
        System.exit(1);
    }



You can rewrite this as 

    Scanner input = new Scanner(System.in);
    System.out.println("Enter number of test cases");

    int remaining = inputInteger(input);
    input.nextLine();


With 

public static void inputInteger(Scanner input) {
    try {
        return input.nextInt();
    } catch (NumberFormatException e) {
        System.err.println("You must enter a number" );
        System.exit(1);
    }
}


This saves having to separate declaration and initialization of the variable.  Now you can do both in one line.  

I prefer the name remaining to numOfTestCases.  Why may become clearer when you see how I use it.  

You don't need to jump through hoops to get an integer with a Scanner.  There's a method just for that.  

Don't overcomplicate things


    boolean validInput = false;
    for (int i = 0; i &lt; numOfTestCases ; i++) {
        validInput = false;

        while (!validInput)
            try {

                String maxEatingInput = input.nextLine();
                String[] individualEatingCaps = maxEatingInput.trim().split("\\s+");
                maxEatingCaps_Limark.add(Integer.parseInt(individualEatingCaps[0]));
                maxEatingCaps_Bob.add(Integer.parseInt(individualEatingCaps[1]));
                validInput = true;

            } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
                System.err.println("Enter in the form: INTEGER:(Limark's Max Eating Capacity) INTEGER:(Bob's Max Eating Capacity)");
                validInput = false;

            }

    }

}



You don't need validInput.  Consider 

    while (remaining &gt; 0) {
        try {
            String maxEatingInput = input.nextLine();
            String[] individualEatingCaps = maxEatingInput.trim().split("\\s+");
            maxEatingCaps_Limark.add(Integer.parseInt(individualEatingCaps[0]));
            maxEatingCaps_Bob.add(Integer.parseInt(individualEatingCaps[1]));
            remaining--;
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            System.err.println("Enter in the form: INTEGER:(Limark's Max Eating Capacity) INTEGER:(Bob's Max Eating Capacity)");
        }
    }


This saves you two variables and a loop and gives the same result.  

Note that you could also drop down to 

    while (remaining &gt; 0) {
        try {
            maxEatingCaps_Limark.add(input.nextInt());
            maxEatingCaps_Bob.add(input.nextInt());
            remaining--;
        } catch (NumberFormatException e) {
            System.err.println("Enter in the form: INTEGER:(Limark's Max Eating Capacity) INTEGER:(Bob's Max Eating Capacity)");
        }
    }


But this doesn't enforce the two numbers per line requirement.  Up to you if you care.  

Equations over iteration

You don't have to iterate through the candy.  You can do the quadratic formula instead.  

$$\frac{(n + 1)^2}{4} = m$$

Where \$n\$ is the number of candies that first bear eats and \$m\$ is the maximum for that bear.  Solving for \$n\$ gives 

$$n = 2\sqrt{m} - 1$$

Rounding down to an odd integer gives the last number of candies that bear can eat. Add 2 to give the first number that it can't eat.  Now do the equivalent for the second bear.  

$$n(n+1) = m$$

And the quadratic formula gives 

$$n = \frac{-1 \pm \sqrt{1 + 4m}}{2}$$

Negative numbers aren't meaningful here, so 

$$n = \frac{\sqrt{4m + 1} - 1}{2}$$

Round down to an even integer to get the last number of candies that can be eaten.  

Whoever gets farther wins.  

Now you just have to turn those equations into Java.