简体   繁体   中英

How do I print the specific coordinates of a letter in a Polybius cipher?

I'm trying to print out the specific coordinates of a letter in a 2d array. It is a Polybius square in an ADVGVX cipher and I want to just print out one position in the array, for instance "a" ie (1,3)

    public char[][] cypher = {
    {'p', 'h', '0', 'q', 'g', '6'}, 
    {'4', 'm', 'e', 'a', '1', 'y'}, 
    {'l', '2', 'n', 'o', 'f', 'd'},
    {'x', 'k', 'r', '3', 'c', 'v'}, 
    {'s', '5', 'c', 'w', '7', 'b'}, 
    {'j', '9', 'u', 't', 'i', '8'},};

I'm trying to do this by using for loops and an if statement.

          public void printArrayElement(){
           for(int row = 0; row < cypher.length; row++){
             for(int column = 0; column < cypher [row].length; column++){
                if (cypher[row][column] == cypher [1][3]){
                    System.out.println(cypher[row][column]);
                  }
                }
              }
            }

I'm not getting any error messages, nothing is happening.

EDIT:

I'm actually having trouble running this as a main method. With just the above I get the message:

Error: Main method not found in class .PolybiusCypher, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application.

When I use public static void main(String[] args) , I get multiple error messages.

public class PolybiusCypher {
    public char[][] cypher = {
        {'p', 'h', '0', 'q', 'g', '6'}, 
        {'4', 'm', 'e', 'a', '1', 'y'}, 
        {'l', '2', 'n', 'o', 'f', 'd'},
        {'x', 'k', 'r', '3', 'c', 'v'}, 
        {'s', '5', 'c', 'w', '7', 'b'}, 
        {'j', '9', 'u', 't', 'i', '8'},};

    public void printArrayElement(){
        System.out.println(cypher[1][3]);
    }

    void main(String[] args) {
        // Need to create instance of PolybiusCypher to access its fields (cypher)
        new PolybiusCypher().printArrayElement();
    }
}

Alternatively you can make cypher and printArrayElement static, and instace creation will not be necessary.

I thought it might be useful to include the code I tested with in the comments. If you place this in a file called PolybiusCipher.java and then try to compile and run it you should see the output 'a' like you expected.

public class PolybiusCipher{

    public static char[][] cypher = {
            {'p', 'h', '0', 'q', 'g', '6'},
            {'4', 'm', 'e', 'a', '1', 'y'},
            {'l', '2', 'n', 'o', 'f', 'd'},
            {'x', 'k', 'r', '3', 'c', 'v'},
            {'s', '5', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'},};

    public static void main(String[] args) {
        printArrayElement();
    }

    public static void printArrayElement(){
        for(int row = 0; row < cypher.length; row++){
            for(int column = 0; column < cypher [row].length; column++){
                if (cypher[row][column] == cypher [1][3]){
                    System.out.println(cypher[row][column]);
                }
            }
        }
    }
}

If you're looking for more information, this answer provides some good information about main methods in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM