I'm new to java and currently in an undergraduate programming course. I've been given an assignment that requires me to create and encryption class then a tester class. In the tester class I am to create two objects of the encryption class: one with the default constructor, then the other with parameterized constructor using two scanner inputs as arguments(password and key). This is what I am having difficulty with. I don't think I am doing this correctly. Here is the code. The error I am receiving when I compile it says "incompatible types, String cannot be converted to int". I thank anyone who can help me.
我是java新手,目前正在攻读本科编程课程。我被赋予了一项任务,要求我创建和加密类,然后是测试器类。在测试器类中,我将创建加密类的两个对象:一个使用默认构造函数,另一个使用参数化构造函数,使用两个扫描程序输入作为参数(密码和密钥)。这就是我遇到的困难。我不认为我这样做是正确的。这是代码。我在编译它时收到的错误是“不兼容的类型,String不能转换为int”。我感谢能帮助我的人。
public class Encryption
{
private int key;
private String encryptedPassword;
Scanner scan = new Scanner(System.in);
public Encryption()
{
key = 0;
encryptedPassword = "";
}
public Encryption(int key, String password)
{
this.key = key;
password = password;
setPassword(password);
}
public void encrypt(String password)
{
char ch;
for(int i=0; i<password.length(); i++){
ch = password.charAt(i);
if (ch >= '!' && ch <= 'z'){
ch = (char)(ch + key);
if (ch > 'z'){
ch = (char)(ch - 'z' + '!' - 1);}
else if (ch < '!'){
ch = (char)(ch +'z' - '!' +1);}
encryptedPassword = password + ch;
}
}
}
public boolean isValidPassword (String password)
{
encrypt(password);
if (password.equals(encryptedPassword))
return true;
else
return false;
}
public String getEncryptedPassword()
{
return encryptedPassword;
}
public void setPassword(String password)
{
encrypt(password);
}
public int getKey()
{
return key;
}
public String toString()
{
return "The encrypted password is " + getEncryptedPassword() + ". " + "The key used to generate this password is " + getKey() + ".";
}
}
Here is the tester class code.
这是测试人员类代码。
import java.util.Scanner;
public class EncryptionTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a password.");
String password = scan.next();
while (password.length() < 8)
{
System.out.println("The password must be at least 8 characters long. Your password is only " + password.length() + " characters long.");
System.out.println("Enter a password.");
password = scan.next();
}
System.out.println("Enter a number between 1 and 10.");
int key = scan.nextInt();
while (key < 1 || key > 10)
{
System.out.println("The key must be between 1 and 10. You entered " + key);
System.out.println("Enter a number between 1 and 10");
key = scan.nextInt();
}
Encryption defaultEncryption = new Encryption();
Encryption argEncryption = new Encryption(password, key);
}
1 个解决方案
#1
2
You are calling the constructor as
你正在调用构造函数
new Encryption(password, key);
when the declaration is
宣言时
public Encryption(int key, String password)
You need to pass in arguments in the same order they are specified in the parameter list. Try
您需要按参数列表中指定的顺序传入参数。尝试
new Encryption(key, password);
#1
2
You are calling the constructor as
你正在调用构造函数
new Encryption(password, key);
when the declaration is
宣言时
public Encryption(int key, String password)
You need to pass in arguments in the same order they are specified in the parameter list. Try
您需要按参数列表中指定的顺序传入参数。尝试
new Encryption(key, password);