Java中数组为null javastringstring数组的复制 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你发出来的代码有两个地方编译错误:1、方法public void readFileByLines()中有return语句2、215行cracker.getKeyWord(ciphertext,keyLength,firstWordLength);调用时的参数和方法定义不匹配 那个return语句是我测试的时候使用的,忘记注释了,但是问题不是这个,关键是dictionary中储存的是null. dictionary数组变量全局的跟函数内的冲突了,还有你定义dictionary数组167964这么长干嘛?如果文件数据少,这个数据里面读到的大部分都是null,这样你后面比较的时候总有null的数组成员会报空指针,业务逻辑要求这样吗?如果确实要求这样,把后面的equals改成strPlain.substring(0, firstWordLength).equals(dictionary[i]),让不为null的数据放equals前面给你大致改了一下:package JUnit;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Scanner;public class VigAlgoCracker { char[] inputTextChar; char[] keyChar; String[] dictionary; final int wordIndex = 167964; int realSize = 0; //IdentityHashMap<String,String> map=new IdentityHashMap<String, String>(); public void setTextChar(String cipherTextInput) { String temp = cipherTextInput.replaceAll(" ", ""); this.inputTextChar = temp.toCharArray(); if (this.inputTextChar.length == 0) { System.out.println("The input is empty!"); return; } } public boolean deCipher(String cipherTextInput, int[] keyInput, int firstWordLength) { setTextChar(cipherTextInput); int length1 = this.inputTextChar.length; int[] cipherDigt = getDigit(length1, this.inputTextChar); int[] plainDigt = getResultDigt(cipherDigt, keyInput); for (int i = 0; i < plainDigt.length; i++) { System.out.print(" " + plainDigt[i]); } System.out.println(); char[] charPlain = new char[plainDigt.length]; for (int i = 0; i < plainDigt.length; i++) { charPlain[i] = (char)(plainDigt[i] + 65); System.out.print(charPlain[i]); } System.out.println(); String strPlain = new String(charPlain); for (int i = 0; i < dictionary.length; i++) { if (strPlain.substring(0, firstWordLength).equals(dictionary[i])) { System.out.print(strPlain); return true; } System.out.println(dictionary[i]); } return false; } public int[] getDigit(int length, char[] textChar) { int[] digit = new int[textChar.length]; for (int i = 0; i < length; i++) { if ((int)textChar[i] < 91 && (int)textChar[i] > 64) { digit[i] = (int)textChar[i] - 65; } else if ((int)textChar[i] > 96 && (int)textChar[i] < 123) { digit[i] = (int)textChar[i] - 97; } else { System.out.println("Your input is invalid,please enter letter only!"); return null; } } return digit; } public int[] getResultDigt(int[] charDigt, int[] keyDigt) { int length1 = charDigt.length; int length2 = keyDigt.length; int[] resultDigt = new int[charDigt.length]; int j = 0; for (int i = 0; i < length1; i++) { if (j < length2) { resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26; if (resultDigt[i] < 0) { resultDigt[i] = resultDigt[i] + 26; } } else { j = 0; resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26; if (resultDigt[i] < 0) { resultDigt[i] = resultDigt[i] + 26; } } j++; } return resultDigt; } public void getKeyWord(String cipherText, int keyLength, int firstWordLength) { int[] key = new int[keyLength]; for (int i = 0; i < keyLength; i++) { key[i] = 65; } int arrIndex = 0; int alphLength = 0; boolean test = false; while (checkKey(key, keyLength)) { while (alphLength < 26) { key[0] = 65 + alphLength; if (deCipher(cipherText, key, firstWordLength) == true) { return; } System.out.println(); alphLength++; } while (arrIndex < keyLength && key[arrIndex] == 90 && checkKey(key, keyLength)) { key[arrIndex] = 65; alphLength = 0; arrIndex++; } if (arrIndex < keyLength) { if (key[arrIndex] < 90) { key[arrIndex]++; arrIndex = 0; } } } } public boolean checkKey(int[] key, int keyLength) { for (int i = 0; i < keyLength; i++) { if (key[i] != 90) { return true; } } return false; } public void readFileByLines(String fileName, int firstWordLength) { File file = new File(fileName); BufferedReader reader = null; dictionary = new String[wordIndex]; try { System.out.println("The files content show by lines:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int index = 0; while ((tempString = reader.readLine()) != null) { if (tempString.length() == firstWordLength) { this.dictionary[index] = new String(tempString); System.out.println(this.dictionary[index]); index++; } } reader.close(); } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } System.out.println(); } public static void main(String[] args) { System.out.println("Please enter the file name or directory:"); Scanner input = new Scanner(System.in); String fileName = input.nextLine(); //String fileName="dict.txt"; System.out.println("Please enter the ciphertext:"); String ciphertext = input.nextLine(); System.out.println("Please enter the first word length:"); int firstWordLength = input.nextInt(); System.out.println("Please enter the key length:"); int keyLength = input.nextInt(); VigAlgoCracker cracker = new VigAlgoCracker(); cracker.readFileByLines(fileName, firstWordLength); cracker.getKeyWord(ciphertext, keyLength, firstWordLength); }} 非常感谢您,问题主要就出在数组中储存了大量的null 值,我把这部分改变了。用另一个实际储存的大小替换了数组的大小,我还有个问题想问一下您,我想把储存方式改成哈希表,这样可以减少后期没有必要的比较次数,问题是我不知道怎么去实现java哈希表中的碰撞问题。比方说有两个单词,一个实absoulute, 另一个是about,我用它们的首字母作为key值,但是一个后一个放入hashmap会覆盖前一个,如何实现向c++哈希链表的方式去储存。谢谢了! 我看你出问题的地方是要实现类似这样的功能:1、读取文件;2、分行保存;3、使用每个条目。如果我说的是对的,我觉得ArrayList<E>是最靠谱的。 多线程问题,高手们进来看看这个怎么设计 求教JasperReport中的“$R”参数是怎么用的 求一算法,实现数组中元素的最优组合! 对于java中继承机制应该怎样理解? 很急在java中取得referrer的问题 如何用drawString把文字打印在图片的中央? 如何用数据库中读出的流创建临时文件?????????? 调查:你所在的软件企业盈利情况调查。 该选择什么开发工具?来者有分! 请问哪里能搞到将JAVA的CLASS文件反编译成源码的工具?给100分呀!!! 读取文件求助 这种验证码怎么识别啊!
1、方法public void readFileByLines()中有return语句
2、215行cracker.getKeyWord(ciphertext,keyLength,firstWordLength);调用时的参数和方法定义不匹配
那个return语句是我测试的时候使用的,忘记注释了,但是问题不是这个,关键是dictionary中储存的是null.
给你大致改了一下:package JUnit;import java.io.BufferedReader;
import java.io.File;import java.io.FileNotFoundException;
import java.io.FileReader;import java.io.IOException;import java.util.Scanner;public class VigAlgoCracker {
char[] inputTextChar;
char[] keyChar;
String[] dictionary;
final int wordIndex = 167964;
int realSize = 0;
//IdentityHashMap<String,String> map=new IdentityHashMap<String, String>(); public void setTextChar(String cipherTextInput) {
String temp = cipherTextInput.replaceAll(" ", "");
this.inputTextChar = temp.toCharArray(); if (this.inputTextChar.length == 0) {
System.out.println("The input is empty!");
return;
} } public boolean deCipher(String cipherTextInput, int[] keyInput,
int firstWordLength) {
setTextChar(cipherTextInput);
int length1 = this.inputTextChar.length; int[] cipherDigt = getDigit(length1, this.inputTextChar); int[] plainDigt = getResultDigt(cipherDigt, keyInput);
for (int i = 0; i < plainDigt.length; i++) { System.out.print(" " + plainDigt[i]);
} System.out.println();
char[] charPlain = new char[plainDigt.length];
for (int i = 0; i < plainDigt.length; i++) {
charPlain[i] = (char)(plainDigt[i] + 65);
System.out.print(charPlain[i]);
}
System.out.println(); String strPlain = new String(charPlain); for (int i = 0; i < dictionary.length; i++) {
if (strPlain.substring(0, firstWordLength).equals(dictionary[i])) {
System.out.print(strPlain);
return true;
}
System.out.println(dictionary[i]); }
return false;
}
public int[] getDigit(int length, char[] textChar) {
int[] digit = new int[textChar.length];
for (int i = 0; i < length; i++) {
if ((int)textChar[i] < 91 && (int)textChar[i] > 64) {
digit[i] = (int)textChar[i] - 65;
} else if ((int)textChar[i] > 96 && (int)textChar[i] < 123) {
digit[i] = (int)textChar[i] - 97;
} else {
System.out.println("Your input is invalid,please enter letter only!");
return null;
}
} return digit;
} public int[] getResultDigt(int[] charDigt, int[] keyDigt) {
int length1 = charDigt.length;
int length2 = keyDigt.length;
int[] resultDigt = new int[charDigt.length];
int j = 0;
for (int i = 0; i < length1; i++) {
if (j < length2) {
resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26;
if (resultDigt[i] < 0) {
resultDigt[i] = resultDigt[i] + 26;
}
} else {
j = 0;
resultDigt[i] = (charDigt[i] - keyDigt[j]) % 26;
if (resultDigt[i] < 0) {
resultDigt[i] = resultDigt[i] + 26;
}
}
j++;
} return resultDigt;
}
public void getKeyWord(String cipherText, int keyLength,
int firstWordLength) { int[] key = new int[keyLength];
for (int i = 0; i < keyLength; i++) {
key[i] = 65; }
int arrIndex = 0;
int alphLength = 0;
boolean test = false; while (checkKey(key, keyLength)) {
while (alphLength < 26) {
key[0] = 65 + alphLength;
if (deCipher(cipherText, key, firstWordLength) == true) {
return;
} System.out.println();
alphLength++;
}
while (arrIndex < keyLength && key[arrIndex] == 90 &&
checkKey(key, keyLength)) {
key[arrIndex] = 65;
alphLength = 0;
arrIndex++;
}
if (arrIndex < keyLength) {
if (key[arrIndex] < 90) {
key[arrIndex]++;
arrIndex = 0;
}
}
} }
public boolean checkKey(int[] key, int keyLength) {
for (int i = 0; i < keyLength; i++) {
if (key[i] != 90) {
return true;
}
}
return false;
} public void readFileByLines(String fileName, int firstWordLength) {
File file = new File(fileName);
BufferedReader reader = null;
dictionary = new String[wordIndex];
try {
System.out.println("The files content show by lines:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int index = 0;
while ((tempString = reader.readLine()) != null) {
if (tempString.length() == firstWordLength) { this.dictionary[index] = new String(tempString);
System.out.println(this.dictionary[index]);
index++;
}
}
reader.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
System.out.println(); }
public static void main(String[] args) {
System.out.println("Please enter the file name or directory:");
Scanner input = new Scanner(System.in);
String fileName = input.nextLine();
//String fileName="dict.txt";
System.out.println("Please enter the ciphertext:");
String ciphertext = input.nextLine();
System.out.println("Please enter the first word length:");
int firstWordLength = input.nextInt();
System.out.println("Please enter the key length:");
int keyLength = input.nextInt();
VigAlgoCracker cracker = new VigAlgoCracker();
cracker.readFileByLines(fileName, firstWordLength);
cracker.getKeyWord(ciphertext, keyLength, firstWordLength); }
}
如果我说的是对的,我觉得ArrayList<E>是最靠谱的。