Trouble decoding the data from the random file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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.



I did this for the encoding part:



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<Character> list = new ArrayList<Character>();

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 < 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< 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 < 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();
}
}


This is for the decoder.



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<Character> list = new ArrayList<Character>();



for(int i = list.size()-1 ; i> 0 ; i--) {
char c = randFile.readChar();
int n = randFile.readInt();
// int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);

for(int m = 0; m < 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();
}

}


My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



this is the question:



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.



The encoding algorithm works as follows:

• 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.

• The last character is followed by the number -1 which indicates the end of the message.

• 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.










share|improve this question

























  • It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

    – Perdi Estaquel
    Nov 23 '18 at 3:15


















0















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.



I did this for the encoding part:



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<Character> list = new ArrayList<Character>();

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 < 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< 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 < 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();
}
}


This is for the decoder.



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<Character> list = new ArrayList<Character>();



for(int i = list.size()-1 ; i> 0 ; i--) {
char c = randFile.readChar();
int n = randFile.readInt();
// int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);

for(int m = 0; m < 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();
}

}


My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



this is the question:



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.



The encoding algorithm works as follows:

• 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.

• The last character is followed by the number -1 which indicates the end of the message.

• 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.










share|improve this question

























  • It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

    – Perdi Estaquel
    Nov 23 '18 at 3:15














0












0








0








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.



I did this for the encoding part:



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<Character> list = new ArrayList<Character>();

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 < 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< 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 < 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();
}
}


This is for the decoder.



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<Character> list = new ArrayList<Character>();



for(int i = list.size()-1 ; i> 0 ; i--) {
char c = randFile.readChar();
int n = randFile.readInt();
// int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);

for(int m = 0; m < 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();
}

}


My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



this is the question:



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.



The encoding algorithm works as follows:

• 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.

• The last character is followed by the number -1 which indicates the end of the message.

• 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.










share|improve this question
















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.



I did this for the encoding part:



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<Character> list = new ArrayList<Character>();

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 < 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< 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 < 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();
}
}


This is for the decoder.



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<Character> list = new ArrayList<Character>();



for(int i = list.size()-1 ; i> 0 ; i--) {
char c = randFile.readChar();
int n = randFile.readInt();
// int z = ThreadLocalRandom.current().nextInt(1, 20 + 1);

for(int m = 0; m < 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();
}

}


My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



this is the question:



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.



The encoding algorithm works as follows:

• 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.

• The last character is followed by the number -1 which indicates the end of the message.

• 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.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 1:16









Nesku

4211412




4211412










asked Nov 22 '18 at 21:38









Houndour99Houndour99

105




105













  • It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

    – Perdi Estaquel
    Nov 23 '18 at 3:15



















  • It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

    – Perdi Estaquel
    Nov 23 '18 at 3:15

















It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

– Perdi Estaquel
Nov 23 '18 at 3:15





It says the decoder prints the output to the console, so you don't need to store those characters, just System.out.print() them.

– Perdi Estaquel
Nov 23 '18 at 3:15












1 Answer
1






active

oldest

votes


















0














My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



The builder you are looking for is a StringBUilder.



Additionally you should look at the skipBytes method of your reader.



Putting those together, your decoder would look something like this (not accounting for edge cases):



   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
StringBuilder builder = new StringBuilder();

while(true) {
char c = randFile.readChar();
builder.append(c);
int n = randFile.readInt();
if(n == -1) break;
randFile.skipBytes(n);

}

//print the output and close the random access file
System.out.println("The data is" + builder.toString());
randFile.close();
}

}





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438244%2ftrouble-decoding-the-data-from-the-random-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



    The builder you are looking for is a StringBUilder.



    Additionally you should look at the skipBytes method of your reader.



    Putting those together, your decoder would look something like this (not accounting for edge cases):



       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
    StringBuilder builder = new StringBuilder();

    while(true) {
    char c = randFile.readChar();
    builder.append(c);
    int n = randFile.readInt();
    if(n == -1) break;
    randFile.skipBytes(n);

    }

    //print the output and close the random access file
    System.out.println("The data is" + builder.toString());
    randFile.close();
    }

    }





    share|improve this answer




























      0














      My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



      The builder you are looking for is a StringBUilder.



      Additionally you should look at the skipBytes method of your reader.



      Putting those together, your decoder would look something like this (not accounting for edge cases):



         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
      StringBuilder builder = new StringBuilder();

      while(true) {
      char c = randFile.readChar();
      builder.append(c);
      int n = randFile.readInt();
      if(n == -1) break;
      randFile.skipBytes(n);

      }

      //print the output and close the random access file
      System.out.println("The data is" + builder.toString());
      randFile.close();
      }

      }





      share|improve this answer


























        0












        0








        0







        My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



        The builder you are looking for is a StringBUilder.



        Additionally you should look at the skipBytes method of your reader.



        Putting those together, your decoder would look something like this (not accounting for edge cases):



           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
        StringBuilder builder = new StringBuilder();

        while(true) {
        char c = randFile.readChar();
        builder.append(c);
        int n = randFile.readInt();
        if(n == -1) break;
        randFile.skipBytes(n);

        }

        //print the output and close the random access file
        System.out.println("The data is" + builder.toString());
        randFile.close();
        }

        }





        share|improve this answer













        My main issue is that I can't figure out a way to store the characters from the randomfile skipping all those random stuff.



        The builder you are looking for is a StringBUilder.



        Additionally you should look at the skipBytes method of your reader.



        Putting those together, your decoder would look something like this (not accounting for edge cases):



           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
        StringBuilder builder = new StringBuilder();

        while(true) {
        char c = randFile.readChar();
        builder.append(c);
        int n = randFile.readInt();
        if(n == -1) break;
        randFile.skipBytes(n);

        }

        //print the output and close the random access file
        System.out.println("The data is" + builder.toString());
        randFile.close();
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 2:30









        AlchemyAlchemy

        4253




        4253
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438244%2ftrouble-decoding-the-data-from-the-random-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?