我有一行文本需要解密为密文。将一行文本从文本文件转换为java中的密文
比方说我的文字的行abc def ghijklm n opq rstu vwx yz
,我想这样的输出:aei qu c k rvzdhmptxbfjn y glosm
可以说,我进入了我的“钥匙”为5.然后,该代码将进入的每5元来自文本文件的文本字符串数组。
这是我想出来的代码,我已经在做什么了。
import java.io.*;
import java.util.*;
public class Files1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int key;
System.out.print("Enter file: ");
String fileName = input.nextLine();
System.out.print("Please enter your Cipher Key: ");
key = input.nextInt();
Scanner inputStream = null;
System.out.println("File name is: " + fileName);
try {
inputStream = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println("Error opening the file" + fileName);
System.exit(0);
}
while (inputStream.hasNextLine()) {
String text = inputStream.nextLine();
System.out.println(text);
char arrayText[] = text.toCharArray();
for (int i = 0; i < arrayText.length; i += key) {
System.out.print("\n" + arrayText[i]);
}
}
}
}
这里是什么在控制台中发生的事情:
Enter file: abc.txt
File name is: abc.txt
abc def ghijklm n opq rstu vwx yz
a
e
i
q
u
我有点不清楚你打算在文本上做什么样的转换。但是,您的循环正在打印出您想要的内容 - 输入的每五个字符。 – 2013-03-22 02:16:04
抱歉不清楚。事情是我不想让我的循环停止,我需要代码来打印所有的文本,不仅是第一个第5个字母,但所有的字母。让我说我有一行文本“1_2_3_4_5”,我的关键是2,我想打印字符串数组的每一个“12345_____”的第二个元素,对不起,我正在编辑它,并试图对它进行校对。 – blueknight 2013-03-22 02:24:19