2016-09-30 80 views
0

我有我的代码问题。我有一个println语句,它以netbeans的形式在输出部分(控制台)输出一个字符串。但是,当我在命令提示符下编译并运行它时,程序将不会打印输出。我用System.out.println("Hello world")替换了System.out.println(plaintext),它工作正常。代码如下。程序将不会打印到命令提示符,但将打印到NetBeans控制台

NetBeans的输出:

gjfjdjddkslskaskdkmfaoadfadvg 

和控制台:

Microsoft Windows [Version 10.0.14393] 
(c) 2016 Microsoft Corporation. All rights reserved. 

C:\Users\KJ4CC>cd C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher 

C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher>javac Cipher.java 

C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src\cipher>cd .. 

C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher\src>java cipher.Cipher 

C:\Users\KJ4CC\OneDrive\Documents\NetBeansProjects\cipher> 

这里是代码:

package cipher; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Scanner; 

/** 
* 
* @author KJ4CC 
*/ 
public class Cipher { 

    char[] text; 

    public static void main(String[] args) { 
     String plainText; 
     String cipherText; 

     Cipher cipher = new Cipher(); 
//these arnt used as of right now. will pass this to the encrypt method, for the text to be encrypted. 
     String original = ""; 
     String iv = "fsf"; 
     String key = "hel"; 

     //calls read file, that will put the text that needs to be encrypted into a char array. 
     plainText = cipher.readFile(iv); 
     cipherText = cipher.encrypt(original, iv, key); 
     System.out.printf(plainText); 
     cipher.output(plainText,cipherText,iv,key); 
    } 

    public String encrypt(String orig, String iv, String key) { 
     int i, j, result; 
     String cipherText = ""; 
     int b = iv.length(); 
     //loops through the entire set of chars 
     /*for (i = 0; i < text.length; i += b) { 
      //Splits the char into block the size of the IV block. 
      for (j = 0; j < b; j++) { 
       if(i == 0){ 
       result = ((iv.charAt(j) - 'a') + (text[j + i] - 'a'))%26; 

       }else{ 
       result = ((key.charAt(j) - 'a') + (text[j + i] - 'a'))%26; 


         } 

      } 
      // System.out.println(", "); 
     } 
*/ 
     return cipherText; 

    } 

    public String readFile(String iv) { 
     String plainText = ""; 
     try { 
      //opens file and reads all of the text into a string. 
      String fileString = new Scanner(new File("text.txt")) 
        .useDelimiter("\\A+").next(); 
      //removes all of the spaces and special chars, to be put into the char array 
      fileString = fileString.replaceAll(" ", ""); 
      fileString = fileString.replaceAll("[',',':','.',' ','(', ')', '--','/','\\s']", ""); 

      //puts the editied string back into a char array for storage. 
      //adding letters too bufer the iv 
      int bufferLetters = fileString.length(); 
      bufferLetters = fileString.length() % iv.length(); 
      if (bufferLetters > 0) { 
       bufferLetters = iv.length() - bufferLetters; 

       for (int i = 0; i < bufferLetters; i++) { 
        fileString = fileString + "x"; 
       } 
      } 
      fileString = fileString.toLowerCase(); 
      plainText = fileString; 
      text = fileString.toCharArray(); 

     } catch (IOException e) { 

     } 
     return plainText; 
    } 
    public void output(String plainText, String cipherText, String iv, String key){ 

    } 
} 

Java版本:

java version "1.8.0_101" 
Java(TM) SE Runtime Environment (build 1.8.0_101-b13) 
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode) 

回答

2

text.txt可能位于NetBeans的文件路径中,但不是在命令提示符下使用javac/java编译/执行时。在后一种情况下,您会收到IOException,因为找不到该文件,但catch块为空。尝试将一个System.out.println()添加到catch块中,并且当您使用独立的javac/java编译/执行时,应该打印该消息。

+0

好点感谢抓住生病的人尽量快速地尝试。 –