Java中的凯撒代码

时间:2015-08-04 14:46:18

标签: java

我正在尝试编写一个解码和编码代码的凯撒代码程序。 它将接收一个文件并返回一个解码或编码文件。

这是我写的:

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

public class cc
{
  public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException
  {
    System.out.println("Welcome to CaesarCipher");
    System.out.print("Enter 1 to encipher, or 2 to decipher (-1 to exit) ");
    Scanner scan = new Scanner(System.in);
    int answer = scan.nextInt();
    System.out.print("What non-negative shift should I use? ");
    Scanner scan1 = new Scanner(System.in);
    int i = scan1.nextInt();
    System.out.print("What is the input file name? ");
    Scanner scan2 = new Scanner(System.in);
    String file = scan2.nextLine();
    System.out.print("What is the output file name? ");
    Scanner scan3 = new Scanner(System.in);
    String savefile = scan3.nextLine();
    if (answer == 1)
    {
      encode(file, savefile, i);
    } else if ( answer == 2)
    {
      decode(file, savefile, i);
    } else if ( answer == -1)
    {
      System.out.println("See you later, aligator!");;
    }
    System.out.println("DONE!");
  }

  public static void encode(String file, String savefile, int i) throws FileNotFoundException, UnsupportedEncodingException
  {
    File f2 = new File(savefile);
    PrintWriter writer = new PrintWriter(f2, "UTF-8");

    i = i % 26 + 26;
    File f = new File(file);
    Scanner input = new Scanner(new FileInputStream(f));
      StringBuilder encoded = new StringBuilder();
      for (char y : file.toCharArray())
      {
        if (Character.isLetter(y))
        {
          if (Character.isUpperCase(y))
          {
          encoded.append((char) ('A' + (y - 'A' + y) % 26 ));
          } else
          {
            encoded.append((char) ('a' + (y - 'a' + y) % 26 ));
          }
         } else
         {
          encoded.append(y);
         }
        writer.print(encoded.toString());
    }
    writer.close();
  }

  public static void decode(String file, String savefile, int i) throws FileNotFoundException, UnsupportedEncodingException
  {
    encode(file, savefile, 26-i);
  }
}

但问题是,输出文件始终为空。 我的代码出了什么问题?

[编辑]

嗨,我添加了writer.close();,但我仍有问题。

当我在输入文件中输入 cows are mowing everyone is happy 时,移位为2,它会为我提供输出

**hhbhbphbpphbppvhbppv.hbppv.fhbppv.fnhbppv.fnf**

这是对的吗?

由于

2 个答案:

答案 0 :(得分:2)

您需要关闭PrintWriter流。

writer.close();

答案 1 :(得分:1)

首先,在编码循环结束后放置write语句:

function capsDetect() {
  var body = document.getElementsByTagName('body')[0];
  var isShiftPressed = false;
  var isCapsOn = null;
  var capsWarning = document.getElementById('caps-lock-warning');
  body.addEventListener('keydown', function(e) {
  var keyCode = e.keyCode ? e.keyCode : e.which;
  if (keyCode = 16){
   isShiftPressed = true;
}
});
body.addEventListener('keyup', function(e) {
 var keyCode = e.keyCode ? e.keyCode : e.which;
 if(keyCode == 16) {
   isShiftPressed = false;
}
 if(keyCode == 20) {
  if(isCapsOn == true) {
   isCapsOn = false;
   capsWarning.style.visibility = 'hidden';
} else if (isCapsOn == false) {
  isCapsOn = true;
  capsWarning.style.visibility = 'visible';
}
}
});
body.addEventListener('keypress', function(e) {
  var keyCode = e.keyCode ? e.keyCode : e.which;
  if(keyCode >= 65 && keyCode <= 90 && !isShiftPressed) {
    isCapsOn = true;
    capsWarning.style.visibility = 'visible';
} else {
    capsWarning.style.visibility = 'hidden';
}
});
}
shiftCaps();

其次,您不能使用文件名(!)来访问文件数据:

for (char y : ...
    ...
} 
writer.print(encoded.toString());
writer.close();

返回包含输入文件名字符的char arrac。逐个阅读文件:

file.toCharArray()