title: Why am I getting an infinite loop of 0's? (Java)
body: <p>I asked a question about my code the other day, which was quickly resolved by the incredible community here. However I have encountered a completely separate problem using a re-written version of my code. Here's a description of the program from my previous post.</p>
  
  <blockquote>
    <p>I'm trying to write a program that can detect the largest sum that can be made with any subset of numbers in an ArrayList, and the sum must be lower than a user-input target number. My program is working flawlessly so far, with the exception of one line (no pun intended). Keep in mind that this code isn't complete yet too. </p>
  </blockquote>
  
  <p>My problem with the code now is that after the user inputs a target number, the program outputs an infinite loop of 0's. Even after trying to debug, I still come up with problems. The ArrayList is being applied to the program perfectly fine, but I think I may have a problem somewhere within one of my <code>while</code> loops. Any ideas?</p>
  
  <p>Heres the code.</p>
  
  <pre><code>import java.util.*;
  class Sum{
      public static void main(String[] args){
          Scanner input = new Scanner(System.in);
          int temp = 0, target = 1, result = 0, firstIndex = 0, secondIndex = 0;
          String tempString = null;
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
          ArrayList&lt;Integer&gt; last = new ArrayList&lt;Integer&gt;();
          System.out.println("Enter integers one at a time, pressing enter after each integer Type \"done\" when finished.\nOR just type \"done\" to use the default list.");
          String placehold = "NotDone";
  
          while (!placehold.equals("done")){
              list.add(input.nextLine());
              placehold = list.get(list.size() - 1);
          }
          list.remove(list.size() - 1);
          if (list.size() == 0){            //Inserts default list if said list is empty
              list.add("1");
              list.add("2");
              list.add("4");
              list.add("5");
              list.add("8");
              list.add("12");
              list.add("15");
              list.add("21");
          }
          for (int i = 0; i &lt; list.size(); i++){
              tempString = list.get(i);
              temp = Integer.parseInt(tempString);     //Changes the items in the list to Integers, which can be inserted into another list and then sorted
              last.add(temp);
          }
          Collections.sort(last);
          System.out.println("Enter the target number");
          target = input.nextInt();
          while (result &lt; target){
              firstIndex = last.size() - 1;
              secondIndex = firstIndex - 1;
              while (last.get(firstIndex) &gt; target){
                  firstIndex--;
              }
              if (last.get(firstIndex) + last.get(secondIndex) &lt; result){
                  result = last.get(firstIndex) + last.get(secondIndex);
                  last.remove(firstIndex);
                  last.remove(secondIndex);
                  last.add(result);
              }
              else{
                  secondIndex--;
              }
              System.out.println(result);
          }  
      }
  }
  </code></pre>
  
  <p>And the Output...</p>
  
  <blockquote>
    <p>Enter integers one at a time, pressing enter after each integer Type "done" when finished.
    OR just type "done" to use the default list.</p>
    
    <p>done    //Prompting to use the default list</p>
    
    <p>Enter the target number</p>
    
    <p>15      //User inputs target number</p>
    
    <p>0
    0
    0
    0
    0
    0
    ... //And so on</p>
  </blockquote>