Peter
Peter

Reputation: 33

Methods arrays in java

I'm practicing for my final exam on java this semester and am doing some problems from the book.

Write a method that returns true if the arrays list1 and list2 are strictly identical, using the following header:

public static boolean equals(int[] list1, int[] list2)

Here's my code. It gives me "illigal start of statement" next to the return statement. What am I doing wrong?

public class StrictlyIdenticalArrays {

    public static void main(String[] args) {

        java.util.Scanner input = new java.util.Scanner(System.in);

        int[] list1 = new int[12];
        int[] list2 = new int[12];

        System.out.print("Enter list1: ");
        for (int i = 0; i < 12; i++)
            list1[i] = input.nextInt();

        System.out.print("Enter list2 ");
        for (int i = 0; i < 12; i++)
            list2[i] = input.nextInt();

        if (equals(list1, list2) == true)
            System.out.print("Two lists are strictly identical");
        else
            System.out.print("Two lists are not strictly identical");
    }

    public static boolean equals(int[] list1, int[] list2) {

        for (int i = 0; i < 12; i++) {
            if (list1[i] == list2[i])

        } return true;
    }
}

Upvotes: 0

Views: 559

Answers (4)

Deep
Deep

Reputation: 98

You method is not really working the way you want it to. You have to change it a little to make it return true or false

You method will look like this:

public static boolean equals(int[] list1, int[] list2) 
{
    for (int i = 0; i < list1.length; i++) 
    {
        if (list1[i] != list2[i])
           return false;
    } 

    return true;
}

Upvotes: 0

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

For a correct implementation of

public static boolean equals(int[] list1, int[] list2)

you need to do the following:

  • First check that the arrays have equal lengths; if not return false.
  • Next, loop through the arrays, using either i < list1.length or i < list2.length as the termination expression for the loop.
  • Within the loop, check that the elements of the arrays are equal; if not return false.
  • Finally, after the loop has been finished, return true.

Upvotes: 1

Mohammed Housseyn Taleb
Mohammed Housseyn Taleb

Reputation: 1828

you have only to use this:

    for (int i = 0; i < 12; i++) {
        if (list1[i] != list2[i])return false;

    } return true;

here you will loop until the end of list is reached when every members are strictly identical and return by true , but if only one member found none identique it return a false. return statement bock method execution whenever is called .

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240996

if (list1[i] == list2[i])

is not closing statement,

it should be closed by brackets after

if (list1[i] == list2[i]) {}

or by semicolon

if (list1[i] == list2[i]);

but more importantly, why are you checking that condition if you are not taking any decision based on that

Upvotes: 0

Related Questions