-1

Trying to fix these problems: Compilation Errors Detected

Line: 94 cannot find symbol symbol: method validateInput(int) location: class Assignment2

Line: 95 the int cannot be dereferenced

Line: 96 array required, but int found

public static void changePasscode(){
    int accountpasscode;
    System.out.println("\nEnter new passcode");
    String stringpasscode = input.next();
    int changePasscode = validateInput(passcode);
    for(int i=0; i<accountpasscode.length; i++) {
        if (accountpasscode[i] == passcode) {
            System.out.println("passcode taken");
            changePasscode();
        }
    }
}
domsson
  • 4,553
  • 2
  • 22
  • 40
  • 2
    Why you are considering "accountpasscode" variable as an array and go through a for loop when it is a single int variable? Could you explain a little bit more the context of your problem? BTW: Try to use the appropriate conventions for naming your variables (lowerCamelCase) – Marcelo Tataje Nov 30 '17 at 13:02
  • Read the errors. _"cannot find symbol symbol: method validateInput(int)"_ pretty much tells you what's wrong, it can't find a method `validateInput(int)`. And in fact, I don't see such a method here. Maybe you didn't show us, maybe it doesn't exist, maybe it has a different signature? You should probably read about (and provide) a [mcve]. – domsson Nov 30 '17 at 13:10
  • You might have ment `validateInput(stringpasscode);` (passing a String instead of an int) – RobCo Nov 30 '17 at 13:24

1 Answers1

1

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 ints. 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).

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • thanks for the answer, but I'm just trying to follow some guidelines for an assignment, I'm not supose to over compilcate the situation, and btw I only need adjusments for my method, I can't take any other method – Dimlead Nov 30 '17 at 16:34
  • @Dimlead Okay. But I don't see the problem then. Just use the hints from the first part. You can also completely adapt the code to the reworked `changePasscode` method. I mean besides `Arrays.equals(first, second)` it doesn't use any foreign methods and if you leave extra-robustness checks, this breaks down to just the last comparison in the shown `areArraysEqual` method (the comparison annotated with "*// Compare element-wise*"). You may want to be more specific about what is left for you or where you have difficulties. – Zabuzard Nov 30 '17 at 18:39