title: Reading in data from a .txt file and printing output separately based on infinite possible integers
body: <p>I apologize if this question has been asked a thousand times before. I tried searching but failed to find what i was looking for. </p>
  
  <p>I'm fairly new to coding, so I am trying to work on a project my sister has been needing at her workplace for some practice. </p>
  
  <p>The program she need seems simple enough, and I have everything figured out but one part (albeit it the most important aspect of the program). Basically, the program she needs scans in data from a .txt file and prints out information in the desired format based on the particular int value that corresponds to the String that is associated with that int. </p>
  
  <p>The text file has two columns and basically looks like this:</p>
  
  <pre><code>Column A     Column B
  Quantity     CPT code
  1            V5008
  1            V5010
  1            V5285
  2            0002M
  2            0058T
  3            0111T
  3            0169T
  </code></pre>
  
  <p>..... so on and so forth. The issue I am having is these lists change every day, as well as the values that are in them, and they are very long (the one I am currently using to create the program is 11185 rows long). Also, there is no way to know what the values in the first column will be, which is how the output is separated. The numbers in the first column could be 1, 2, 16, 147, 8000... could be any number. </p>
  
  <p>The output needs to be printed separately based on these numbers. For instance, the output for the above would look like this:</p>
  
  <pre><code>If IsCPT("V5008,V5010,V5285") Then
     If Quantity &gt; 1 AND Allowance &gt; 0.0 Then
        HoldBill("MUE Edits")
     End If
  
  ElseIf IsCPT("0002M,0058T") Then
     If Quantity &gt; 2 AND Allowance &gt; 0.0 Then
        HoldBill("MUE Edits")
     End If
  
  ElseIf IsCPT("0111T,0169T") Then
     If Quantity &gt; 3 AND Allowance &gt; 0.0 Then
        HoldBill("MUE Edits")
     End If
  </code></pre>
  
  <p>I'm having trouble with keeping the strings in the second column separate from each other so it doesn't all print out together. Right now, I am storing the second column in an String array, and once everything is read in, looping through and printing each index. </p>
  
  <p>How would you go about keeping the second column separated based on the first column, when the first column could be any number? Would you use a 2d array, storing the int value in first index of each row and filling up the rows with the associated Strings? </p>
  
  <p>I'm sorry for the long-winded question, but I wanted to be as thorough as possible. If anyone who would like to help needs to see some of the code I have written, I would be happy to post it. Just let me know!</p>
  
  <p>Thank you in advance for any help!</p>
  
  <p>Edit: Thanks for the responses. Below is what my class looks like, which is responsible for scanning in the data. </p>
  
  <pre><code>import java.util.Arrays;
  import java.io.File;
  import java.io.IOException;
  import java.util.Scanner;
  import java.util.ArrayList;
  
  public class Processor {
  
  private String[] finalArray;
  private int numberOfCpt;
  private int quantity;
  private int i = 1;
  private int length;
  private int length1;
  private int lineLength;
  
  public Processor() {
    finalArray = new String[0];
  }
  
  public void addCptToArray(String cptCodeIn) {
    finalArray = Arrays.copyOf(finalArray, finalArray.length + 1);
  
    length = finalArray.length;
    finalArray[length - 1] = cptCodeIn;  
  }
  
  public void newArray() {
    length = finalArray.length;
  
    finalArray = Arrays.copyOf(finalArray, finalArray.length - length);
  }
  
  public int getNumberOfCpt() {
    return numberOfCpt;
  }
  
  public void readFile(String fileIn) throws IOException {
  
  String fileName = fileIn;
  Scanner scan = new Scanner(new File(fileName));
  scan.nextLine();
  
  while (scan.hasNext()) {
  
  String file = scan.nextLine();
  Scanner scan2 = new Scanner(file);
  quantity = scan2.nextInt();
  String cpt = scan2.next(); 
  
  for (i = i; i &lt;= finalArray.length + 1;) {
    if (quantity == i) { 
     addCptToArray(cpt);
     break;
  }
  
    if (quantity != i) {
     i++;
   }
  }
  
  numberOfCpt = finalArray.length - 1;     
  } 
  
  
  public String generateReport() {
  String output = "";
  String output1 = "";
  output += "If ";
  String line1 = "";
  
  for (int j = 0; j &lt; finalArray.length; j++) {
     output1 += finalArray[j] + ",";
  }
  
  for (String line : charLimiter(output1, 320)) {
     lineLength = line.length();
     length1 = line.length() - 6;
  
  if (line.substring(length1, lineLength).contains(",")) {
     line1 = line.substring(length1, lineLength).replace(",", "");
     line = line.substring(0, length1);
     line = line.concat(line1);
  }
  
  if (lineLength == 318) { 
  
     output += "IsCPT" + "(" + "\"" + line 
                       + "\""  + ")" + " or\n\n";
  }
  
  else {
     output += "IsCPT" + "(" + "\"" + line + "\""  
                       + ")" + " Then";
  
     output += "\n   If Quantity &gt; " + quantity + " AND Allowance &gt;" 
                       + " 0.0 Then \n      HoldBill(\"MUE Edits\")"
                       + "\n   End If";
    }
  }
  
  return output;
  }
  
  private static ArrayList&lt;String&gt; charLimiter(String input, int limit) {
  ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();
  String[] parts = input.split(",");
  StringBuilder sb = new StringBuilder();
    for (String part : parts) {
      if (sb.length() + part.length() &gt; limit) {
        list.add(sb.toString().substring(0, sb.toString().length()));
        sb = new StringBuilder();
    }
       sb.append(part + ",");
  }
  
  if (sb.length() &gt; 0) {
       list.add(sb.toString());
  }
  
  return list;
  }
  </code></pre>
  
  <p>I know the for loop under readFile() looks wonky(as does probably the rest of the program). I have been trying every possible way I could think of to get this working, so some of the variables and methods were created but unused. </p>