2014-11-05 196 views
0

我試圖讀取一個文本文件,並將每個字母「加密」/從ASCII錶轉換爲+1(我也想「解密」,所以-1) 。所以「a」將變成「b」,「b」變成「c」等等。我只需要轉換字母(忽略其他所有內容,按照原樣打印)。我有麻煩與這部分代碼:加密/解密文件。用於加密/解密的ASCII +1

​​

我已經想通了如何+1炭,但我不知道如何添加回數組或打印出來正確(因爲「morewords.add(ch)」只會添加字符,而不是轉換所有的字符添加一個字符串)。 「words.get(i).length()」取整數組「words」的整個長度,當我只想將字符串@位置「i」的長度放在數組中時,它會拋出錯誤,因爲長度數組的長度比字符串長。我一直堅持這幾個小時,我無法弄清楚。我在想,也許我不應該以字符串的形式閱讀它們,應該以字符的形式閱讀它們,這可能會更簡單?

public static void main(String[] args) { 

     Scanner in = new Scanner(System.in); 
     ArrayList<String> words = new ArrayList<String>(); 
     ArrayList<Character> morewords = new ArrayList<Character>(); 
     String fileName = ""; //Replace Test with this 
     File f; 
     Scanner fileIn; 

     System.out.println("Please enter a file name for encryption: "); 
     //fileName = in.nextLine(); 
     fileName = "Test.txt"; 


     try 
     { 
      //Build the file and attach a scanner to it 
      f = new File (fileName); 
      fileIn = new Scanner (f);    

      System.out.println(f.exists()); //For errors 
      int counting = 0; 

      //Reads in indvidual strings. 
      for(counting =0; fileIn.hasNext(); counting++) 
      {     
       words.add(fileIn.next()); 
       System.out.println(words); 
      }    

      PrintWriter fileOut = new PrintWriter ("Backwards.txt"); 

      for(int i = 0; i <= words.size(); i++) 
      {     
       for(int j = 0; j <= words.get(i).length(); j++) 
       { 
        char ch = ' '; 
        ch = words.get(i).charAt(j); 
        ch += 1;      
        morewords.add(ch); 
       }    
       fileOut.print(morewords.get(i) + " "); 
      }    

      fileOut.close();    

     } 
     catch(FileNotFoundException e) 
     { 
      System.out.println("Couldn't find file"); 
     } 

    } 
+0

基於安德烈的回答,這似乎是一個錯字。是嗎? – 2014-11-05 13:22:33

回答

2

首先在一個for循環是對你最後的長度爲1的

什麼我已經改變做

for (int i = 0; i <= words.size()-1; i++){} 

如果找你從0開始的

PrintWriter fileOut = new PrintWriter("C:/Backwards.txt"); 
     for (int i = 0; i <= words.size()-1; i++) 
     { 
      for (int j = 0; j <= words.get(i).length()-1; j++) 
      { 
       char ch = ' '; 
       ch = words.get(i).charAt(j); 
       ch ++; // +=1 
       morewords.add(ch); 
       fileOut.print(ch); 
      } 
      fileOut.print(" "); 
     } 

     fileOut.close(); 

它輸出正確,如果我理解正確=)

這是我的代碼

public static void main(String[] args) throws Exception 
{ 
    BufferedReader inChannel = new BufferedReader(new FileReader("C:/script.txt")); 
    BufferedWriter outChannel = new BufferedWriter(new FileWriter("C:/output.txt")); 
    String toParse = ""; 
    while ((toParse = inChannel.readLine()) != null) 
    { 
     String toWrite = ""; 
     for(int i=0; i!=toParse.length();i++) 
     { 
      char c = toParse.charAt(i); 
      if(true) //check if must be encoded or not 
      { 
       c++; 
       toWrite += c; 
      } 
     } 
     outChannel.write(toWrite); 
     outChannel.newLine(); 
    }  
    inChannel.close(); 
    outChannel.close(); 
} 

希望幫助

+0

這確實有幫助,謝謝!將-1添加到words.size()解決了問題。此外,感謝您向我展示了一種更簡單,更簡單的方法來做到這一點! – Riotson 2014-12-03 03:41:34

+0

很高興聽到它! =) – 2014-12-03 10:46:58