我正在学习超类,抽象类和抽象类,所以我必须从文件中读取一个单词并依赖于我找到的单词,让我的程序运行三种不同的方法之一。但是,我总是遇到这个例外:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: int cannot be converted to java.lang.String
at encryption.EncryptorFactory.create(EncryptorFactory.java:23)
at superencryptor.SuperEncryptor.main(SuperEncryptor.java:15)
C:\Users\Linda\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53:
这是我试图运行该程序的主要包:
package superencryptor;
import encryption.EncryptorFactory;
import encryption.*;
public class SuperEncryptor
{
public static void main(String[] args)
{
String strTestData = "This is a test";
/* V3 - using a factory */
EncryptorFactory obFactory = new EncryptorFactory();
IEncryptor obEncryptor = obFactory.create();
String strOutEnc = obEncryptor.encrypt(strTestData);
System.out.println("The encrypted value is: " + strOutEnc);
String strOutDec = obEncryptor.decrypt(strOutEnc);
System.out.println("The decrypted value is: " + strOutDec);
}
}
以下是我尝试运行的方法:
package encryptor;
public class BasicEncryptor implements IEncryptor
{
@Override
public String encrypt(String strData) // "abc"
{
String strEncrypted = "";
for (int nIndex = 0; nIndex < strData.length(); ++nIndex)
{
strEncrypted += strData.charAt(nIndex);
strEncrypted += "@";
}
return strEncrypted;
}
@Override
public String decrypt(String strData) // "a@b@c@"
{
String strDecrypted = "";
// Start at the first character (index = 0) and add 2 to it each time
// through to skip the accompanying @ symbol.
for (int nIndex = 0; nIndex < strData.length(); nIndex += 2)
{
strDecrypted += strData.charAt(nIndex);
}
return strDecrypted;
}
}
这是我的界面:
package encryptor;
public interface IEncryptor
{
public String encrypt(String strData);
public String decrypt(String strData);
}
工厂类:
package encryptor;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class EncryptorFactory
{
public IEncryptor create() throws IOException
{
IEncryptor obEncryptor = null;
FileReader fileReader = new FileReader ( "C:\\config.dat");
try
{
BufferedReader br = new BufferedReader(fileReader);
String line = br.readLine();
if ( line.equals ("BasicEncrypto")&& br != null)
{
obEncryptor= new BasicEncryptor ();
}
br.close();
}
catch (IOException e)
{
System.err.println("Error: " + e);
}
catch (NumberFormatException e)
{
System.err.println("Invalid number");
}
return obEncryptor;
}
}
答案 0 :(得分:0)
您真正需要的是在代码中添加类转换以将int类型转换为字符串类型。以下是您的示例:
String sTest = Integer.toString(2);