user527619
user527619

Reputation: 89

Streams,Buffer in Java

I have exams in java and when I study I saw this exercise and I try to solve it but I found some difficulty so please help me Consider the following comment, header and partial code for a method in a utility encryption class called Atbash.

    /**
    * Prompts the user for the pathname of a source .txt file to encrypt.
    * Prompts the user for the pathname of a destination .txt file
    * to which the encrypted text will be written.
    * The pathnames are then used to create two File objects.
    * The method then attempts to open streams on both files, one to
    * read from the source file, the other to write to the destination file.
    *
    * The method then reads from the read stream a line at a time.
    * Each line read is converted to uppercase using the message
    * toUpperCase(). The method then constructs an encrypted string
    * into which all the alphabetic characters from the line are
    • substituted by their atbash equivalent, however any numeric or
    * punctuation characters are simply added unchanged to the encrypted
    * string. After each string is constructed it is written to the write
    * stream followed by a newline character.
    */
    public static void encryptFile()
    {
    OUDialog.alert("Please choose a file to encrypt");
    String pathname = OUFileChooser.getFilename();
    File aFile = new File(pathname);
    OUDialog.alert("Please give a file name for the encrypted file");
    String pathname2 = OUFileChooser.getFilename();
    File bFile = new File(pathname2);
    BufferedReader bufferedFileReader = null;
    BufferedWriter bufferedFileWriter = null;

    try
    {
    String currentLine;
    bufferedFileReader = new BufferedReader(new FileReader(aFile));
    bufferedFileWriter = new BufferedWriter(new FileWriter(bFile));
    String codedLine = "";
    char currentChar;
    String alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String mirror ="ZYXWVUTSRQPONMLKJIHGFEDCBA";
    currentLine = bufferedFileReader.readLine();
    while (//Boolean condition to be written as your answer for part (i))
/**this what I do while(currentLine!=null)*/

    {
    //Statement block to be written as your answer for part (ii)

    }
    }
    catch (Exception anException)
    {
    System.out.println("Error: " + anException);
    }
    finally
    {
    try
    {
    //Statement block to be written as your answer for part (iii)
  /* bufferedFileReader.close();
   *  bufferedFileWriter.close();
   */

    }
    catch (Exception anException)
    {
    System.out.println("Error: " + anException);
    }
    }
    }

(i) Write down the boolean condition for the while loop in the above method.

while(currentLine= bufferedFileReader.readLine()!= null)
    {

    }

(ii) Write the code for the while statement block in the above method. This should ensure that when the while loop has terminated, the destination .txt file contains an encrypted version of the source .txt file. Your code should make use of the variables bufferedFileReader, bufferedFileWriter, currentLine, codedLine, currentChar, alphabet and mirror. You may find the following two messages from the protocol of String useful: charAt(int) which returns the char value at the index specified by the argument and indexOf(char) which returns the index of the first occurrence of the argument within the receiver.

(iii) Write the code for the final try block.

bufferedFileReader.close();
 bufferedFileWriter.close();

Upvotes: 0

Views: 326

Answers (1)

Péter Török
Péter Török

Reputation: 116266

Here

/**this what I do while(currentLine!=-1)*/

since currentLine is a String, you can't compare it to an int value. You should check the Javadoc of BufferedReader.readLine() to see how it indicates that there is no more data to read, then fix the condition accordingly.

Upvotes: 2

Related Questions