Identical Array program - error missing return statement

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 855 Views - Last Post: 08 October 2014 - 03:30 PM Rate Topic: -----

#1 guinea222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-September 14

Identical Array program - error missing return statement

Posted 07 October 2014 - 08:05 PM

Hi there, I am trying to write a program where I input numbers and if both arrays contain the same numbers it prints out identical. If not it prints out not identical. The issue I am having is that it says I am missing return statement at the very end of my program (the last bracket).

I added the else at the very end before the return true and this was the error that occurred. However is I remove the else all it says is lists are not identical even if they are? I have no idea what is going on. I appreciate any help!


import java.util.*;
 import java.util.Scanner;
import java.util.Arrays;

public class Tester {

	public static void main(String[] args) {
	    java.util.Scanner input = new java.util.Scanner(System.in);
	 
	    // Enter values for list1
	    System.out.print("Enter list1: ");
	    int size1 = input.nextInt();
	    int[] list1 = new int[size1];
	 
	    for (int i = 0; i < list1.length; i++)
	      list1[i] = input.nextInt();
	 
	    // Enter values for list2
	    System.out.print("Enter list2: ");
	    int size2 = input.nextInt();
	    int[] list2 = new int[size2];
	 
	    for (int i = 0; i < list2.length; i++)
	      list2[i] = input.nextInt();
	 
	    if (equal(list1, list2)) {
	      System.out.println("Two lists are identical");
	    }
	    else {
	      System.out.println("Two lists are not identical");
	    }
	  }
	 
	     public static boolean equal(int[] list1, int[] list2) {
	 
	      if(list1.length == list2.length) {
	           Arrays.sort(list1);
	           Arrays.sort(list2);
	           // Don't return quite yet
	       }
	    
              else {
	           return false; // If the lengths aren't the same, then they're not the same.   
              }
	      // Now test if every element is the same
	      for (int i = 0; i < list1.length; i++) 
	          if (list1[i] != list2[i]){
	              return false; // If one is wrong then they all are wrong.
	      }
	       else
	      // If all these tests worked, then they are identical.
	      return true;
            
	     }




Is This A Good Question/Topic? 0
  • +

Replies To: Identical Array program - error missing return statement

#2 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 07 October 2014 - 08:53 PM

There are a couple things I'm not liking in your code:

For starters you've already imported Scanner, so just create a scanner as:
Scanner input = new Scanner(System.in);


Now to your code and why it is not working. Honestly, you have too much going on: you do not need that many if-else statements to get the job done. Furthermore, you're missing a return statement because you're trying to make every if-else loop return something, so you're running out of possibilities on what to return (it's a bit more complicated than that, but that's the simplest way of explaining it) and when to return 'it'. Have you considered just initializing a boolean variable that holds the value of true or false and returning it isn't of 'true' or 'false'?

    public static boolean equal(int[] list1, int[] list2) {
        boolean isEqual = false;


With this you'd be able to remove all the if-else loops and just keep the main one: the one that checks their lengths. Once you've checked if their lengths are exactly the same you can have your program sort each array. Now that they're sorted you can use your for-loop to check their original values; it's exactly how you currently have it. EXCEPT you're setting the boolean variable that you created to 'false' or 'true'. This way the loop isn't just instantly returning; it's going through each index of the array(s) and changing the variable isEqual based on if they are same:

            for (int i = 0; i < list1.length; i++) {
                if (list1[i] != list2[i]) {
                    isEqual = false;
                } else
                    isEqual = true;
            }
        }


After that it's simply a matter of returning the variable at the end. Problem solved. If you need help understanding something feel free to ask.
Was This Post Helpful? 1
  • +
  • -

#3 guinea222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-September 14

Re: Identical Array program - error missing return statement

Posted 07 October 2014 - 09:14 PM

I switched my code with the advice you gave me, but I am not fully understanding and probably made my code more of a mess. I am not sure if I am even replying correctly to your post.




View PostInvoker, on 07 October 2014 - 10:53 PM, said:

There are a couple things I'm not liking in your code:

For starters you've already imported Scanner, so just create a scanner as:
Scanner input = new Scanner(System.in);


Now to your code and why it is not working. Honestly, you have too much going on: you do not need that many if-else statements to get the job done. Furthermore, you're missing a return statement because you're trying to make every if-else loop return something, so you're running out of possibilities on what to return (it's a bit more complicated than that, but that's the simplest way of explaining it) and when to return 'it'. Have you considered just initializing a boolean variable that holds the value of true or false and returning it isn't of 'true' or 'false'?

    public static boolean equal(int[] list1, int[] list2) {
        boolean isEqual = false;


With this you'd be able to remove all the if-else loops and just keep the main one: the one that checks their lengths. Once you've checked if their lengths are exactly the same you can have your program sort each array. Now that they're sorted you can use your for-loop to check their original values; it's exactly how you currently have it. EXCEPT you're setting the boolean variable that you created to 'false' or 'true'. This way the loop isn't just instantly returning; it's going through each index of the array(s) and changing the variable isEqual based on if they are same:

            for (int i = 0; i < list1.length; i++) {
                if (list1[i] != list2[i]) {
                    isEqual = false;
                } else
                    isEqual = true;
            }
        }


After that it's simply a matter of returning the variable at the end. Problem solved. If you need help understanding something feel free to ask.

Was This Post Helpful? 0
  • +
  • -

#4 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 07 October 2014 - 09:17 PM

No problem Guinea. I'm more than happy to help :)

Let's start by looking at your method equal();
You've created your boolean variable isEqual and set it to false; this will be what is getting returned and will represent whether or not the two arrays match.

Now, before we go on you haven't added your Array.sort methods; no biggie I'll add them for you in the code below. Carefully read the lines and try to understand how they 'flow'

        boolean isEqual = false;
//your first condition
// both arrays must be the same length, otherwise they cannot match at all
// if this if statement were to fail the sort, the for loop and everything within the if statement will not be invoked.
// thus at the end you will return the variable isEqual, which was originally false.
        if(list1.length == list2.length) {
            Arrays.sort(list1);
            Arrays.sort(list2);
            for (int i = 0; i < list1.length; i++) {
                if (list1[i] != list2[i]) {
                    isEqual = false;
                } else
                    isEqual = true;
            }

Your call to this method is within an if statement so if the lengths of the two arrays were not to match the if statement call of this method would be false (because of the variable isEqual) so the else statement would invoke printing out that the arrays are not identical.
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:04 AM

Quote

Once you've checked if their lengths are exactly the same you can have your program sort each array.

Why sort? That doesn't seem to be part of the requirement or at least it has not been explicitly mentioned that order is unimportant?

This post has been edited by g00se: 08 October 2014 - 05:06 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#6 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:07 AM

I wanted to make use of what he currently was using, rather than give him something a bit too different from his current work.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:10 AM

OK, it remains to be seen whether order IS important. But a couple of observations

a. The last-posted code will return true if only the last element is the same in each
b. There is java.util.Arrays.equals

This post has been edited by g00se: 08 October 2014 - 05:11 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#8 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:26 AM

Oh, nice point. I didn't notice that at the time. I did, however, write a code using array.equals, but, like I said, I didn't want to change his code too much so I used isEqual.

Question though: assuming the sort works 100% of the time, wouldn't checking if the last element is the same make the it hold true? If there's a case that defies this: please explain as I'm still learning myself and it would be great to know.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:38 AM

Quote

Question though: assuming the sort works 100% of the time, wouldn't checking if the last element is the same make the it hold true?
Err .. i think the answer to that question is yes ;)/> But it's a wrong question. Given what i just said (which was a hint btw) then the code returns true for the following two arrays (shown sorted). The question is why, and what can be done about it?

10,20,30,40
17,19,23,40

This post has been edited by g00se: 08 October 2014 - 05:44 AM

Was This Post Helpful? 0
  • +
  • -

#10 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 05:46 AM

Gonna be honest here: this is completely a guess, but is it because they're all int values and the array is the same length?

This post has been edited by Invoker: 08 October 2014 - 05:47 AM

Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 06:13 AM

Your difficulty is caused by not having a grasp of what the algo is doing, coupled with a firm grasp of its purpose. What do you do when you don't know what an algo is doing?
Was This Post Helpful? 0
  • +
  • -

#12 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 07:15 AM

g00se, I'm afraid you've lost me from the start. All this time, have we been applying those integers to the last code I posted? Cause I know why those arrays come out as the same; the final iteration of the loop checks the last value if they are equal and sets the variable to true - I get that much.
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 07:39 AM

Quote

Cause I know why those arrays come out as the same; the final iteration of the loop checks the last value if they are equal and sets the variable to true - I get that much.

Yes, and of course that's what i stated. The point is: is that what's required to fulfil the purpose of the algo? If not, in what way are we going to fix it?
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3447
  • View blog
  • Posts: 15,614
  • Joined: 20-September 08

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 08:27 AM

Invoker, please don't PM me about this. Discussion on this topic belongs to the OP and is equally instructive for him/her
Was This Post Helpful? 0
  • +
  • -

#15 Invoker  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: Identical Array program - error missing return statement

Posted 08 October 2014 - 03:18 PM

That could have been said in a reply, but okay. Prior experiences tell me that discussions within - other - forums' topics were not appropriate forum etiquette; it isn't in most forums I've been in. However, I can see how our discussion might be beneficial to the OP and future readers, so I apologize for that mishap.

Continuing the discussion--

I see how my question is the wrong question. There are two instances in which the program could print that the arrays are similar. In fact, there may be more that I haven't realized.

a.) The user could enter within an array multiple ints of the same value (ex. 2, 2, 3, 4, 5 || 1, 2, 3, 4, 5). This would cause the array to sort it correctly, but if the last digit would make it true despite there not being a 1 in the first array.

b.) The program could sort the values, but because the loop continues chugging forward, it assigns true to the variable because of matching last values and print that they're the same.

Both cases are very similar, but slightly different.


After inserting a break statement to stop the loop from setting values for true or false the first time the outcome is false, I'm not getting any logic errors. Would this work is there something else?

Also, I get what you mean when you say a sort isn't necessary, and assuming the list was already sorted I could figure it out pretty easily. However, this isn't the case, and I'm unsure on how to check whether or not a value occurs in the array, but based on how many times it occurs in the other. (Ex. user inputs (2, 2, 2, 3, 4 || 1, 2, 3, 4). I'm not an expert and realize that more practice is needed for me to excel in my understanding. Would this particular method work?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2