0

I am trying to pass a value to an attribute of the class that I created in Java but it just won't work.

Here is my code :

// Landscape Class

public class Landscape{

// Attribute

char [] [] landscape ;
    
// Constructor to create the landscape 

public void Landscape(){

landscape = {

    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
    
    };

}

// Function to display the landscape

public void display(){

    System.out.println("");

for(char [] element : landscape){


    System.out.print("              ");
    
    for(char character : element){
        System.out.print(character);
    }
    
    System.out.println("");
    
}

System.out.println("");

}

public static void main(String[] args){
    
    Landscape landscape = new Landscape();
    landscape.display();
}


}
 

I have already chacked my array syntax and I know it's fine but for some reasons it seems that Java can't recognize my class attribute and gives me all these crazy errors :

Landscape.java:13: error: illegal start of expression
landscape = {
            ^
Landscape.java:15: error: not a statement
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
     ^
Landscape.java:15: error: ';' expected
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
        ^
Landscape.java:15: error: illegal start of expression
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:16: error: not a statement
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:16: error: ';' expected
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:16: error: illegal start of expression
    {'^',' ',' ',' ',' ','^','^','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:17: error: not a statement
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
     ^
Landscape.java:17: error: ';' expected
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
        ^
Landscape.java:17: error: illegal start of expression
    {'^',' ','^','^',' ',' ',' ','^','^',' ','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:18: error: not a statement
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:18: error: ';' expected
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:18: error: illegal start of expression
    {'^',' ',' ','^',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:19: error: not a statement
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
     ^
Landscape.java:19: error: ';' expected
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
        ^
Landscape.java:19: error: illegal start of expression
    {'^','^',' ','^',' ','^',' ','^','^','^','^','^','^','^','^','^'},
                                                                     ^
Landscape.java:20: error: not a statement
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:20: error: ';' expected
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:20: error: illegal start of expression
    {'^',' ',' ',' ',' ',' ',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:21: error: not a statement
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
     ^
Landscape.java:21: error: ';' expected
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
        ^
Landscape.java:21: error: illegal start of expression
    {'^','^',' ',' ','^','^',' ','^','^',' ',' ',' ',' ','^','^','^'},
                                                                     ^
Landscape.java:22: error: not a statement
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
     ^
Landscape.java:22: error: ';' expected
    {'^','^','^','^','^','^','^','^','^','^','^','^','^','^','^','^'}
        ^
24 errors 

I would like to understand what I did wrong and how to fix it

4
  • That's not a constructor. You're also trying to access non-static functions without one. Commented Dec 3, 2020 at 5:59
  • Have no idea why this question got downvoted, but it looks like an interesting case of array initialization in Java, that eventually does not allow to use array initializers like that. Honestly, I was not aware of such specifics, and it's interesting why it was done like that, and I cannot see a real reason of it since compiler seems to be able to detect the variable type itself. Perhaps this one explains in details: stackoverflow.com/questions/12805535/… Commented Dec 3, 2020 at 7:19
  • @Abra I tried your solution which did allow me to compile without error but I now have a NullPointerException when I run the code.. My guess is that the solution you provided create a new multi-arrays char but since it is in the scope of the method my method display() cannot access it which is why I wanted to make an attribute "landscape" and use it in my methods but that didn't work Commented Dec 3, 2020 at 8:33
  • @Monstarules Why ? Commented Dec 3, 2020 at 8:34

1 Answer 1

1

Constructor declarations do not contain a return type.

public class Landscape {
    char[][] landscape;

    // Constructor to create the landscape
    public Landscape() {
        landscape = new char[][]{
                {'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', ' ', ' ', '^', '^', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', ' ', '^', '^', ' ', ' ', ' ', '^', '^', ' ', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', '^', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', ' ', '^', ' ', '^', ' ', '^', '^', '^', '^', '^', '^', '^', '^', '^'},
                {'^', ' ', ' ', ' ', ' ', ' ', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', ' ', ' ', '^', '^', ' ', '^', '^', ' ', ' ', ' ', ' ', '^', '^', '^'},
                {'^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^', '^'}
        };
    }

    // Function to display the landscape
    public void display() {
        System.out.println("");
        for (char[] element : landscape) {
            System.out.print("              ");
            for (char character : element) {
                System.out.print(character);
            }
            System.out.println("");
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        Landscape landscape = new Landscape();
        landscape.display();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the information although as I said this does not help me with my attribute and my exception mentioned higher
My bad forgot to re-add the "new char[] []" it works fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.