2013-03-22 98 views
0

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

我有點不清楚你打算在文本上做什麼樣的轉換。但是,您的循環正在打印出您想要的內容 - 輸入的每五個字符。 – 2013-03-22 02:16:04

+0

抱歉不清楚。事情是我不想讓我的循環停止,我需要代碼來打印所有的文本,不僅是第一個第5個字母,但所有的字母。讓我說我有一行文本「1_2_3_4_5」,我的關鍵是2,我想打印字符串數組的每一個「12345_____」的第二個元素,對不起,我正在編輯它,並試圖對它進行校對。 – blueknight 2013-03-22 02:24:19

回答

0

不指定應該發生的空間是什麼,或者需要什麼環繞時發生,但假設空間是顯著和環繞恰好自然:

for (int i = 0; i < text.length(); i++) 
{ 
    System.out.print(text.charAt((i*5) % text.length())); 
} 

打印aei qu c k rvzdhmptxbfjn y glosw,這強烈暗示您的預期輸出錯誤。

+0

抱歉不清楚。空間作爲數組的一部分包含在內。 – blueknight 2013-03-22 03:28:45

+0

我剛剛測試了你的代碼,並調整了一下以得到確切的答案,我發現它更短更有效!非常感謝你! – blueknight 2013-03-22 13:15:23

1

你需要的是一個圓形的列表。

這是使用數組的一個非常簡單和粗糙的循環列表實現。

import java.util.Iterator; 
import java.util.List; 

public class CircularList implements Iterator<String> { 

    private String[] list; 

    private int pointerIndex; 

    private int key; 

    public CircularList(String[] list, int key) { 
     this.list = list; 
     pointerIndex = 1 - key; 
     this.key = key; 
    } 

    @Override 
    public boolean hasNext() { 
     if(list.length == 0){ 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String next() { 
     if(pointerIndex + key > list.length) { 
      int diff = (list.length-1) - pointerIndex; 
      pointerIndex = key - diff; 
      return list[pointerIndex]; 
     }else { 
      pointerIndex = pointerIndex + key; 
      return list[pointerIndex]; 
     } 
    } 

    @Override 
    public void remove() { 
     //Do Nothing 
    } 

} 

一旦你可以在其中以循環方式遍歷一個列表,你可以將現有的實施改變你到這一點 -

import java.io.*; 
import java.util.*; 

public class Files1 { 

    public static void main(String[] args) { 

     System.out.print("Enter file: "); 
     Scanner input = new Scanner(System.in); 
     String fileName = input.nextLine(); 
     Scanner inputStream = null; 
     System.out.println("" + 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); 

      String[] splits = text.split(""); 
      CircularList clist = new CircularList(splits, 5); 

      for (int i = 0; i < splits.length -1; i += 1) { 
       System.out.print("" + clist.next()); 
      } 

     } 
    } 

} 

輸出 -

Enter file: resources\abc.txt 
resources\abc.txt 
abc def ghijklm n opq rstu vwx yz 
aei qu c k rvzdhmptxbfjn y glosw 

也是最後密碼中的字符應該是'w'而不是'm'。

+0

這基本上是我在找什麼!我只是添加了一些調整,以獲得確切的答案即時尋找!謝謝! – blueknight 2013-03-22 03:15:42

+0

矯枉過正。模運算符是他所需要的,而不是45行代碼中的新類。 – EJP 2013-03-22 05:20:54

0

import java.io. ; import java.util。;

公共類文件下載1 {

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); 
     for (int i = 0; i < text.length(); i++) { 
      System.out.print(text.charAt((i * key) % text.length())); 
     } 

    } 
} 

}

非常感謝EJP和PAI!

我學到了很多東西!