我有一行文本需要解密爲密文。將一行文本從文本文件轉換爲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