2013-03-22 96 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!

我学到了很多东西!