title: Removing Text from an Array Made From a File in Java
body: <p>I am relatively knew to Java and, for a personal project, I am trying to write a program that can take user input to write to a text file and remove text from a line (indicated by the array's index) from an array built from the same text file so that when you print out the contents of the file, what you removed is, indeed, removed.</p>
  
  <p>Here is my code, I apologize for any other mistakes I cannot see:</p>
  
  <pre><code>public class Writing {
      Scanner input = new Scanner(System.in);
      File customerFile = new File("Customers.txt");
      String[] fileArray;
  
  public Writing() throws IOException{
      FileReader fileReader = new FileReader(customerFile);
      BufferedReader bufferedReader = new BufferedReader(fileReader);
      List&lt;String&gt; lines = new ArrayList&lt;String&gt;();
      String line = null;
  
      while ((line = bufferedReader.readLine()) != null) {
          lines.add(line);
      }
      bufferedReader.close();
      fileArray = lines.toArray(new String[lines.size()]);
  }
  public void FileWrite() throws IOException {
  
          String inputData = input.nextLine();
          if (!customerFile.exists()) {
              customerFile.createNewFile();
  
          }
          FileWriter fileWriter = new FileWriter(customerFile.getName(),true);
          BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
          bufferWriter.write(inputData + "\n");
          bufferWriter.close();
  
          System.out.println("Done.");
  }
  
  //Here is the one I have trouble with
  public void RemoveFile() throws IOException {
      LinkedList&lt;String&gt; cusList = new LinkedList&lt;String&gt;(Arrays.asList(fileArray));
  
      System.out.println("Which one would you like to delete? [0-#].");
      //then it prints the list so you can see what you want to delete
  
          for(String x: cusList){
              System.out.println(x);
          }
  
              String removeThis = input.nextLine();
              int removeNum = Integer.parseInt(removeThis);
              if()){ //if the user input contains the number that occurs as an index in the list
                  //not sure what to put here
                      fileArray = cusList.toArray(fileArray);
  
              }else{
                  System.out.println("Remove failed.");
              }
  }
  }
  </code></pre>
  
  <p>I'm stuck because despite other past attempts to solve this, I end up receiving my else statement or with no changes to the text file.</p>
  
  <p>EDIT: 
  Here are the main attempts I made to solve this issue, but to no avail. <strong>This is within the RemoveFile() class.</strong></p>
  
  <p><strong>Failed Method:</strong> </p>
  
  <pre><code>    //tries to remove the element if it is found, before I decided to jusr remove the index
      //example: if "Cocoa Powder" is typed in and it exists within the list of the array created from the file, remove it.
  
  if(cusList.contains(removeThis)){
  cusList.remove(removeThis);
  cusList.toArray(fileArray);
  }
  //the else statement here saying that it failed
  </code></pre>
  
  <p><strong>Failed Method 2:</strong></p>
  
  <pre><code>   //attempts to remove the content of the inputted index within the list 
  
  if(removeNum.equals(cusList.indexOf(removeNum))){
      cusList.remove(removeNum);
      cusList.toArray(fileArray);
  }
  
  //else statement would go here
  </code></pre>
  
  <p>Help!</p>
  
  <p><strong>Solution</strong>:
  I figured out it out, thanks to all who contributed to help, and I realized that an easier solution is to clear all the contents of the text file, with an array made with all of the contents, and then writing those contents back into the text file after all of the changes. Here is my code:</p>
  
  <pre><code>   // RemoveFile method
      public void RemoveFile() throws IOException {
          LinkedList&lt;String&gt; cusList = new LinkedList&lt;String&gt;(
              Arrays.asList(fileArray));
  
      System.out.println("Which one would you like to delete? Each customer is given a number [1-#].");
      System.out.println("The file will change when program terminated.");
      for (int i = 1; i &lt; fileArray.length; i++) {
          for (String x : cusList) {
              System.out.println("Customer " + i + ": " + x);
              i++;
          }
      }
  
      String removeThis = input.nextLine();
      int removedNum = Integer.parseInt(removeThis);
      int removeNum = removedNum - 1;
      if (removeNum &gt;= 0 &amp;&amp; removeNum &lt; cusList.size()) {
          cusList.remove(removeNum);
          fileArray = cusList.toArray(fileArray);
  
          PrintWriter writer = new PrintWriter(customerFile);
          writer.print("");// clears the file contents
          writer.close();
  
          FileWriter fileWriter = new FileWriter(customerFile.getName(), true);
          BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
  
          for (String x : cusList) {
              bufferWriter.write(x + "\n");
  
          }
          bufferWriter.close();
      } else {
          System.out.println("Index out of bounds, " + removeNum + " &gt;= "
                  + cusList.size());
      }
  </code></pre>
  
  <p>This seemed to work perfectly and MadProgrammer's suggestion made the logic work, instead of continuously returning false and then allowing the program to extract the data from the file into an array, clear the contents of the array, and then write it back into the file.</p>