当前位置: 代码迷 >> J2SE >> Java中数组为null,该如何解决
  详细解决方案

Java中数组为null,该如何解决

热度:12   发布时间:2016-04-23 19:59:22.0
Java中数组为null
我的代码中想使用string数组去储存从文件中读取的字符串,我使用全局变量dictionary[]这个字符串数组在public String[] readFileByLines(String fileName, int firstWordLength)这个方法中复制tempString中的值,测试之后发现dictionary[]中可以得到所有的值。但是当我在 public boolean deCipher(String cipherTextInput, int[] keyInput,int firstWordLength,String[] dictionary)这个方法中再次测试dictionary[]中的值的时候,发现全部是空。我知道这可能存在只想地址,让后tempString被销毁,所以dictionary指向空的可能。但是我不明白怎样才能避免这种情况。 急求大家帮忙。代码贴在下面,谢谢了!大家主要帮忙看一下readFileByLines和deCipher这两个方法就好了。



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++){
            f(dictionary[i].equals(strPlain.substring(0,firstWordLength))){
                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,String[] dictionary) {
        
        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 (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }

      
      
 


        System.out.println();






     

        return dictionary;

    }


    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);
        
}

------解决思路----------------------
你发出来的代码有两个地方编译错误:
1、方法public void readFileByLines()中有return语句
2、215行cracker.getKeyWord(ciphertext,keyLength,firstWordLength);调用时的参数和方法定义不匹配
------解决思路----------------------
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);

    }
}



  相关解决方案