How to fix illegal start of expression compilation error?

theredfox24

Write a method that takes a 2D array of integers as an argument. The array returns a new 2D array of integers of the same size (same number of rows and columns). The values in the returned array are the same as those in the argument array EXCEPT that any negative values are made positive.

My method will not compile the compiler keeps complaining about my method that its illegal start of expression. It appears to fails to recognize that I have written a method whats so ever.

This is what I have done:

public class Examprep1
{
    public static void main (String[] args)
    {
        int [][] array = new int[2][4];
        array[0][0]=1;
        array[0][1]=5;
        array[0][2]=-7;
        array[0][3]=9;
        array[1][0]=-2;
        array[1][1]=4;
        array[1][2]=6;
        array[1][3]=-8;

        int x;

        // Using for loops to create the 2D array
        for (int rows=0; rows<2; rows++)
        {
            for (int cols=0; cols<4;cols++)
            {
                System.out.print( array[rows][cols]+ " ");
            }
            System.out.println("");
        }
        System.out.println(newArray(array));

        private static int[][] newArray(int[][] old)
        {
            int y;
            int[][] current = new int [2][4];

            for (int rows=0; rows<2; rows++)
            {
                for (int cols=0; cols<4;cols++)
                {   
                    if (old[rows][cols]<0)
                    {
                        y=Math.abs(old[rows][cols]);
                        old[rows][cols] = (int)Math.pow(old[rows][cols],2)/x;
                    }
                    old[rows][cols] = current[rows][cols];
                 }  
            }
            return current;
        }
    }
}
user1211

There are lot of compilation issue. You should starting writing code step by step.

Here is the working model: (Advice you for reference)

class Examprep1 {
    public static void main(String[] args) {
        int[][] array = new int[2][4];
        array[0][0] = 1;
        array[0][1] = 5;
        array[0][2] = -7;
        array[0][3] = 9;
        array[1][0] = -2;
        array[1][1] = 4;
        array[1][2] = 6;
        array[1][3] = -8;

        int x = 0;

        // Using for loops to create the 2D array
        System.out.println("Printing old array");
        printArray(array);

        System.out.println("Printing new  array");
        printArray(newArray(array, x));

    }

    private static void printArray(int[][] a) {
        for (int rows = 0; rows < 2; rows++) {
            for (int cols = 0; cols < 4; cols++) {

                System.out.print(a[rows][cols] + " ");
            }
            System.out.println("");
        }

    }

    private static int[][] newArray(int[][] old, int x) {
        int y;
        int[][] current = new int[2][4];

        for (int rows = 0; rows < 2; rows++) {
            for (int cols = 0; cols < 4; cols++) {

                y = Math.abs(old[rows][cols]);
                current[rows][cols] = y;

            }

        }
        return current;
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I keep getting an "illegal start of expression" error on my code

error: illegal start of expression in Android Studio Code

Java Error: illegal start of expression

Illegal start of expression for Annotations

javac strange syntax - error illegal start of expression

Compile Error: illegal start of expression

Why is this an illegal start of expression?

Error illegal start of simple expression

How would I fix "Illegal start of expression"?

How to fix the "Illegal reference to memory A" error

PHP how to fix Illegal offset type error?

Receiving Illegal start of expression error

Illegal start of expression error in Java

Illegal start of expression (method)

What's wrong with my java program, "illegal start of expression error"

Netbeans is giving me "illegal start of expression" error message on my methods

'Try without catch error', ')' expected, illegal start of expression..?

Weird "illegal start of expression" error in Netbeans

java illegal start of expression error

java error in for loop condition evaluation: not a statement, illegal start of expression

"Illegal start of expression" error in Java

Error code compiling: illegal start of expression

Error: illegal start of expression followed by PriorityQueue

Running into an illegal start of expression error while changing the value of an array

error: illegal start of expression in my Android studio code

Error in code, "illegal start of expression"

How to fix Illegal return statement error?

how to fix this verilog part-select error: Illegal operand for constant expression

How do I fix these errors in java: illegal start of expression public static void numCheck(){ <identifier> expected public class void numCheck(){