0

I am searching for the lines of code to create a new array using a method called insertRow(int[] row). With this method, users can insert 5 numbers to form an array. Then this array should be named row2. Please help.

public class App 
{

    public static void main(String[] args) 
    {
        int[] row = new int[5];
        int[] row1 = {2,7,1,9,4};
        //int[] row2 = insertRow(row); this is wrong
    }

    public static void insertRow(int[] row)
    {
        for (int i = 0; i < row.length; i++)
        {
            int number;
            do
                number = Integer.parseInt(JOptionPane.showInputDialog("Insert the " + (i+1) + "th positif number"));
            while (getal < 0);

            row[i] = number;
        }
    }
}

1 Answer 1

2

You were on the right track: change the signature of your method to return int[], allocate the row inside, and put your code in place of ... below:

public static int[] insertRow() {
    int[] row = new int[5];
    ...
    return row;
}

Now this will work:

int[] row2 = insertRow(); // this is no longer wrong :)
Sign up to request clarification or add additional context in comments.

1 Comment

I think that he named the method "insertRow" because he wants to keep what's already in the array. Your method replaces the old array with a new one.

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.