title: Trouble decoding the data from the random file
body: <p>I have to make a program that reads input from a file, encodes it with random characters and integers into a random file and then decode the randomfile to print the original data from the input file in the console. </p>
  
  <p>I did this for the encoding part:  </p>
  
  <pre><code>public class Encoder implements IEncoder {
  
      public void encode(String inputFileName, String outputFilePath) throws IOException{
          File file = new File(inputFileName);
  
          //load characters into the character array 
          char[] chars = {'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'};
  
          RandomAccessFile randFile = new RandomAccessFile(outputFilePath, "rw");
  
          ArrayList&lt;Character&gt; list = new ArrayList&lt;Character&gt;();
  
          String k = "";
  
          //scan  input file, save into string 
  
          try {
              Scanner scan = new Scanner(file);
              while(scan.hasNextLine()) {
                  k=k+scan.nextLine();
              }
  
          scan.close();
          } catch (FileNotFoundException e) {
              System.out.println("There was an issue with the file...");
          }
  
          //save data from the input file into the ArrayList 
  
          for(int i = 0; i &lt; k.length(); i++) {
              list.add(k.charAt(i));
          }
  
          //write each character into a binary file, along with a random integer n, followed by n random characters
  
          for(int j = 0; j&lt; list.size()-1; j++) {
              int n = ThreadLocalRandom.current().nextInt(1, 20 + 1);
              randFile.writeChar(list.get(j));
              randFile.writeInt(n);
  
              for (int m = 0; m &lt; n; m++) {
                  int z = ThreadLocalRandom.current().nextInt(1, 11 + 1);
                  randFile.writeChar(chars[z]);
              }
          }
  
          randFile.writeChar(list.get(list.size() - 1));
          randFile.writeInt(-1);
          randFile.close();
      }
  }
  </code></pre>
  
  <p>This is for the decoder. </p>
  
  <pre><code>public class Decoder implements IDecoder {
  
      @Override
      public void decode(String filePath) throws IOException {
  
          //read the random access file 
          RandomAccessFile randFile = new RandomAccessFile(filePath, "r");
  
          //create a string to print the output to console
          String k ="";
  
          //initialize the array list 
          ArrayList&lt;Character&gt; list = new ArrayList&lt;Character&gt;();
  
  
  
          for(int i = list.size()-1 ; i&gt; 0 ; i--) {
              char c = randFile.readChar();
              int n = randFile.readInt();
              // int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);
  
              for(int m = 0; m &lt; n; m++) {
                  // int x = ThreadLocalRandom.current().nextInt(1, 11 + 1);
                  k = k + c;
              }
          }
  
          //print the output and close the random access file 
          System.out.println("The data is" + k);
          randFile.close();
      }
  
  }
  </code></pre>
  
  <p>My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff. </p>
  
  <p>this is the question:</p>
  
  <p>You are to write a Java program to encode and decode a text file. The encoder reads a message stored in a plain text file, encodes it, and stores it in a binary file. The decoder reads the binary file, decodes the message and prints it to the console. </p>
  
  <p>The encoding algorithm works as follows:<br>
  •   Each character c in the message is followed by a randomly-generated number n ranging from 1 to 20. n is the number of bytes of random data between c and the next character in the message. So, after writing c followed by n to the file, there should be n byte locations (with random data) before the next character c is written.<br>
  •   The last character is followed by the number -1 which indicates the end of the message.<br>
  •   Each character stored in the binary file occupies 2 bytes while each integer occupies 4 bytes. Random data is stored between the integer following each character and the next character.  </p>