Explanation
Line: 94 cannot find symbol symbol: method validateInput(int) location: class Assignment2
The compiler complaints that it does not find a method with signature
int validateInput(int)
inside your Assignment2
class. Since you wrote
int changePasscode = validateInput(passcode);
Check the name, the return and input types (both int
) and the location of your method (is it inside Assignment2
?). Especially make sure that it is int
and not int[]
which would be an array of int
s. You also probably wanted the method to accept a String
instead of int
since I assume it will parse the passcode as text and return the int[]
representation of it.
Line: 95 the int cannot be dereferenced
You wrote
accountpasscode.length
So essentially you are trying to call a variable length
that is inside the object accountpasscode
. However accountpasscode
is an int
which is a primitive datatype and not an Object
. Thus it has no methods and also no callable variables.
Line: 96 array required, but int found
The compiler complaints about accountpasscode[i]
since you wrote
if (accountpasscode[i] == passcode) {
The variable accountpasscode
is of type int
but a syntax like variable[x]
is only valid for arrays, so of type int[]
for example.
Solution
All in all you probably messed up your types. An accountpasscode
should probably be an array of some int
values like
// Representing 123827
{ 1, 2, 3, 8, 2, 7 }
Therefore you need int[]
(an array):
int[] accountpasscode;
// and
int[] changePasscode
And your validateInput
method should thus also return an int[]
, i.e. having the following signature:
int[] validateInput(String)
Also note that you never use the variable changePasscode
, you probably intended to assign it to accountpasscode
instead. So you may drop the variable changePasscode
:
accountpasscode = validateInput(passcode);
Also inside your check you want to check value-wise. Since passcode
should also be an array int[]
you need to also access its elements:
for (int i = 0; i < accountpasscode.length; i++) {
// Condition changed
if (accountpasscode[i] == passcode[i]) {
System.out.println("passcode taken");
changePasscode();
}
}
And additionally you probably wanted to check all elements and only if all are equal decide for "passcode taken". You can do this by using the Arrays#equals
method:
if (Arrays.equals(accountpasscode, passcode) {
System.out.println("passcode taken");
changePasscode();
}
And completely drop your for
loop then. Alternatively, for practice, a manual equivalent approach:
public boolean areArraysEqual(int[] first, int[] second) {
// Only one is null
if ((first == null && second != null)
|| (first != null && second == null) {
return false;
}
// Both are null
if (first == null && second == null) {
return true;
}
// Length is different
if (first.length != second.length) {
return false;
}
// Compare element-wise
for (int i = 0; i < first.length; i++) {
// Found a different pair
if (first[i] != second[i]) {
return false;
}
}
// All pairs are equal
return true;
}
Reworked code
So all in all it could look like this:
public PasscodeStore {
private int[] passcode;
public PasscodeStore(String initialPasscodeText) throws IllegalArgumentException {
passcode = PasscodeStore.validateInput(initialPasscodeText);
}
public void changePasscode() throws IllegalArgumentException {
int[] candidate = new int[0];
// Use try-with-resource for resource management
try (Scanner input = new Scanner(System.in)) {
boolean passcodeTaken = false;
do {
System.out.println("\nEnter new passcode.");
String candidateText = input.nextLine();
int[] candidate = PasscodeStore.validateInput(candidateText);
// Reject candidate if it's equal
// to the current password
passcodeTaken = Arrays.equals(candidate, passcode);
if (passcodeTaken) {
System.out.println("Passcode already taken.");
passcodeTaken = true;
}
// Repeat until passcode is not taken
} while (passcodeTaken);
}
// Candidate is not taken, set it
passcode = candidate;
}
public static int[] validateInput(String input) throws IllegalArgumentException {
int[] result = new int[input.length()];
int i = 0;
for (char ch : input.toCharArray()) {
if (!Character.isDigit(ch)) {
throw new IllegalArgumentException("Input must contain only digits.");
}
// Char can correctly be converted
// to int by casting
result[i] = (int) ch;
i++;
}
}
}
Just as a note. Telling the user whether a password is already taken leaks confident data into a public result. A bad user (like the nasty Eve) which just enters some random passwords thus knows which are already taken (poor Bob).