【发布时间】:2015-05-02 21:03:31
【问题描述】:
我正在尝试使用 StringBuilder 制作凯撒密码,但我遇到了 2 个主要问题。一个是我的加密和解密不会相互抵消,另一个是当我使用 .isLetter() 函数时,它实际上会跳过很多字符,而不仅仅是忽略空格。谁能帮帮我?
import java.io.*;
import java.util.Scanner;
import javafx.scene.transform.ScaleBuilder;
public class FileUtilities
{
static String tempE ;
static String tempD ;
public static void main(String [] args)
{
try
{
File iFile = new File("BattlePlans.txt");
File oFile= new File("decodedBattlePlans.txt");
Scanner input = new Scanner(iFile);
PrintWriter fout = new PrintWriter(oFile);
while(input.hasNextLine())
{
tempE = input.nextLine();
//System.out.println(args[0]);
//StringBuilder str = new StringBuilder(args[1]);
//My encrypt funtion give me an indexoutofbounds error for some reason and I don't know why do you see what I did wrong
if(args[0].equals("encrypt"))
{
fout.println(encrypt(tempE, Integer.parseInt(args[1])));
}else if(args[0].equalsIgnoreCase("decrypt"))
{
fout.println(decrypt(tempE, Integer.parseInt(args[1])));
}
}
input.close();
fout.close();
}
catch(FileNotFoundException e)
{
}
// this is commented out becuase I couldn't figure out
//how to make the InvalidCommand method or InvalidShift methods
//and it was keeping it from running
//do I even have to make my own, or is there something built in I can use?
/*catch(InvalidCommand i)
{
System.out.println("Invalid command");
}
catch(InvalidShift s)
{
System.out.println("Invalid shift");
}*/
}
public static String encrypt(String encrypt, int shift)
{
//first SB takes in the original text and second SB holds the changed txt
StringBuilder str = new StringBuilder(encrypt);
StringBuilder str2 = new StringBuilder();
for(int i = 0; i < str.length(); i++ )
{
// I want to use a isLetter() if statement here to that it won't take out my space characters but when I did it made it print nothing so I deleted it
char s = encrypt.charAt(i);
//checks for capital and lowercase letter to see what the shift range is
if(Character.isLetter(i))
{
if(s >= 'A' && s <= 'Z')
{
//I went to the ILC to ask how to handle the shift and I could just add it to the end
//I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
//the subtract the shift. It shifts the text to the left actually. is that okay?
int x = s - 'A' - shift;
x = x % 26;
str2.append((char)(x + 'A'));
// add to str with .append
}else if(s >= 'a' && s <= 'z')
{
int z = s - 'a' - shift;
z = z % 26;
str2.append((char)(z + 'a'));
}
}
}
return str2.toString();
}
public static String decrypt(String decrypt, int shift)
{
//first SB takes in the original text and second SB holds the changed txt
StringBuilder str = new StringBuilder(decrypt);
StringBuilder str2 = new StringBuilder();
for(int i = 0; i < decrypt.length(); i++)
{
char s = decrypt.charAt(i);
//same if statement for encrypt would go here to so that it would keep the whitespaces
if(Character.isLetter(i))
{
if(s >= 'A' && s <= 'Z')
{
//I went to the ILC to ask how to handle the shift and I could just add it to the end
//I was told that I couldn't and had to use another int and subtract the bottom of the range from the character at the index i
//the subtract the shift. It shifts the text to the left actually. is that okay?
int x = s + 'A' + shift;
if(x < 0)
x = x + 26;
str2.append((char)(x + 'A'));
// add to str with .append
}else if(s >= 'a' && s <= 'z')
{
int z = s + 'a' + shift;
if(z < 0)
z = z + 26;
z = z % 26;
str2.append((char)(z + 'a'));
}
}
}
//decrypt is encrypt backwards
return str2.toString();
}
}
【问题讨论】:
-
isLetter函数识别字母而非非空白字符。查看isWhitespace函数。 -
是的,但是我想检查它是否是一个字母,然后在if语句中做代码。
标签: java