-1

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" i.e. (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.

Ishaan Javali
  • 1,655
  • 3
  • 11
  • 22
Kiva
  • 23
  • 4
  • 1
    Why do you have a for loop? Just `System.out.println(cypher[1][3]);` would be sufficient to print out (1, 3). – Compass Jul 17 '19 at 21:03
  • I ran your code and got the output 'a'. Is it possible that you're not running your method from a main method? – AbsoluteSpace Jul 17 '19 at 21:06
  • @AbsoluteSpace Hey thanks a million. I'm actually having trouble running this as a main method, I'm getting a few error messages, I'll clarify above. – Kiva Jul 17 '19 at 22:30
  • `void main(args)` is the only way to root program execution in java, you have to have it somewhere. "I get multiple error messages" is not going to help us much, my guess would be you try to call `printArrayElement` directly from `main` or replacing `printArrayElement` with `main`. Try `void main(String[] args) {new PolybiusCypher().printArrayElement();}`. – Vojtěch Kaiser Jul 17 '19 at 22:37

2 Answers2

0
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.

Vojtěch Kaiser
  • 558
  • 2
  • 14
0

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.

AbsoluteSpace
  • 662
  • 2
  • 9
  • 21