Syntax & elements

What’s the syntax of the following statement, “If an array element matches any of the elements of another array, then…”? I need it for my checkType() method. It’s a basic question, but a google search, albeit a perfunctory one, didn’t yield any results

UPD: So I now have this crazy new checkType() method. I decided to compare the contents of the two arrays using nested for loops (they used this procedure to traverse two-dimensional arrays in Learn Java). The method is called on an object with String stringValue and String type fields. “im” stands for “integer mutant”, it’s supposed to be assigned to objects whose stringValue starts with an integer or a dot, but continues with something else. “bi” is for “big” integers (of more than ten). “i” is for regular integers. “d” is for decimals. “r” is for Romans. “rm” is for “Roman mutants”. “g” is for other types of garbage input :slightly_smiling_face:. I inputted an object with a stringValue of 2.5, and it returned “im” instead of “d”. For some reason, it didn’t find the dot in the standardArabChars array, and the condition charArray[j] != standardArabChars[y] was true. Could you tell me why it happened and how to fix it? But generally speaking, the initial question still stands

public String checkType() {
        char[] standardArabChars = new char[11];
        for(int i = 0; i < standardArabChars.length - 1; i++){
            standardArabChars[i] = (char)('0' + i);
        }
        standardArabChars[10] = '.';

        char[] standardRomanChars = new char[26];
        for(int i = 0; i < standardRomanChars.length; i++){
            standardRomanChars[i] = (char)('A' + i);
        }
        char[] charArray = stringValue.toCharArray();

        for(int i = 0; i < standardArabChars.length; i++){
            if(charArray[0] == standardArabChars[i]){
                for(int j = 1; j < charArray.length; j++){
                    for(int y = 0; y < standardArabChars.length; y++){
                        if(charArray[j] != standardArabChars[y]){
                            return type = "im";
                        } else if(charArray[j] == '.') {
                            return type = "d";
                        } else {
                            if(Integer.decode(stringValue) > 10) {
                                return type = "bi";
                            } else {
                                return type = "i";
                            }
                        }
                    }
                }
            }
        }

        for(int i = 0; i < standardRomanChars.length; i++){
            if(charArray[0] == standardRomanChars[i]){
                for(int j = 1; j < charArray.length; j++){
                    for(int y = 0; y < standardRomanChars.length; y++){
                        if(charArray[j] != standardRomanChars[y]){
                            return type = "rm";
                        } else {
                            return type = "r";
                        }
                    }
                }
            }
        }
      return type = "g";
    }

As a general bit of advice, you ought to give some thought to being considerably more liberal with your use of System.out.println(). If you’re struggling to understand what your program is doing, having it tell you what is going on by writing to the terminal is brilliantly useful.

It is, in fact, what I have done to get to the root of your problem. Your logic is wrong.

I’ve added a load of println() calls at every key point in your program - every for and every if - so I can see the state of the code and how it is determining the outcome. I get this:

Comparing charArray[0] (2) to standardArabChars[0] (0).
Comparing charArray[0] (2) to standardArabChars[1] (1).
Comparing charArray[0] (2) to standardArabChars[2] (2).
IF Condition Met for standardArabChars[2]!
Inside double-nested for! Current variables are:
j: 1 | y: 0 | charArray[1]: . | standardArabChars[0]: 0
IF Condition Met: charArray[j] != standardArabChars[y]!
im

Your program has, correctly, identified that . is not the same as 0 and so has acted as instructed to return im.

3 Likes

You won’t believe it, but I finally did it. All on my own! It may be clumsy, and I’m sure the same thing can be achieved in a less verbose manner, but it works! What do you think?

    public String checkType() {
        char[] standardArabChars = new char[11];
        for (int i = 0; i < standardArabChars.length - 1; i++) {
            standardArabChars[i] = (char) ('0' + i);
        }
        standardArabChars[10] = '.';

        char[] standardRomanChars = new char[52];
        for (int i = 0; i < 26; i++) {
            standardRomanChars[i] = (char) ('A' + i);
        }
        for (int i = 26; i < standardRomanChars.length; i++) {
            standardRomanChars[i] = (char) ('a' + (i - 26));
        }

        char[] charArray = stringValue.toCharArray();

        int dSepCount = 0, dSepIn = 0, nonstandardCharIn = -1, mismatch;

        for (int i = 0; i < standardRomanChars.length; i++) {
            if (standardRomanChars[i] == charArray[0]) {
                for (int j = 0; j < charArray.length; j++) {
                    mismatch = 0;
                    for (int y = 0; y < standardRomanChars.length; y++) {
                        if (standardRomanChars[y] != charArray[j]) {
                            mismatch++;
                            if (mismatch == standardRomanChars.length) {
                                return type = "rm";
                            }
                        }
                    }
                }
                return type = "r";
            }
        }

        for (int i = 0; i < standardArabChars.length; i++) {
            if (standardArabChars[i] == charArray[0]) {
                for (int j = 0; j < charArray.length; j++) {
                    mismatch = 0;
                    for (int y = 0; y < standardArabChars.length; y++) {
                        if (standardArabChars[y] != charArray[j]) {
                            mismatch++;
                        }
                        if (mismatch == standardArabChars.length) {
                            nonstandardCharIn = j;
                        }
                    }
                    if (charArray[j] == '.') {
                        dSepCount++;
                        if (dSepCount == 1) {
                            dSepIn = j;
                        } else if (dSepCount > 1 && (charArray[0] != '.' || charArray[1] != '.')) {
                            return type = "dm";
                        }
                    }
                }
                if (nonstandardCharIn == -1) {
                    if (dSepCount == 0) {
                        return type = "i";
                    } else if (dSepCount == 1) {
                        char[] zeroCharArray = new char[charArray.length - dSepIn - 1];
                        Arrays.fill(zeroCharArray, '0');
                        char[] postDSep = Arrays.copyOfRange(charArray, dSepIn + 1, charArray.length);
                        if (Arrays.equals(postDSep, zeroCharArray)) {
                            return type = "i";
                        } else if (charArray[charArray.length - 1] != '.') {
                            return type = "d";
                        }
                    }
                } else {
                    if(dSepCount == 0) {
                        return type = "im";
                    } else {
                        char[] postDSepPreNonstandardChar = Arrays.copyOfRange(charArray, dSepIn + 1, nonstandardCharIn);
                        char[] zeroCharArray = new char[postDSepPreNonstandardChar.length];
                        Arrays.fill(zeroCharArray, '0');
                        if (Arrays.equals(postDSepPreNonstandardChar, zeroCharArray)) {
                            return type = "im";
                        } else {
                            return type = "dm";
                        }
                    }
                }
            }
        }
        return type = "g";
    }

Thank you! Please check out how I did it (it’s another reply to this post). I’m curious what you think about it