我在java考试,当我学习时,我看到了这个练习,我试着解决它,但我发现有些困难所以请帮助我 请考虑实用程序中方法的以下注释,标题和部分代码 加密类称为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)在上述方法中记下while循环的布尔条件。
while(currentLine= bufferedFileReader.readLine()!= null)
{
}
(ii)在上述方法中编写while语句块的代码。这应该 确保当while循环终止时,目标.txt文件包含 源.txt文件的加密版本。你的代码应该使用 变量bufferedFileReader,bufferedFileWriter,currentLine,codedLine, currentChar,字母和镜子。 您可能会发现以下两个来自String协议的消息很有用: charAt(int),它返回参数指定的索引处的char值 和indexOf(char)返回第一次出现的索引 接收器内的参数。
(iii)编写最终try块的代码。
bufferedFileReader.close();
bufferedFileWriter.close();
答案 0 :(得分:2)
下面
/**this what I do while(currentLine!=-1)*/
由于currentLine
是String
,因此您无法将其与int
值进行比较。您应该检查BufferedReader.readLine()
的Javadoc以查看它是如何表示没有更多数据要读取,然后相应地修复条件。