Method works, value not returned.
Dear all:
This method works as needed, but the value is not returned and I can not understand why.
The method checks the first list against the second list and tells you how many species on the first list are not on the second list.
QUESTION/S
So the question is:
In the compareTwoLists method. Why is the value of 2, assigned to the results variable, not returned by the method?
Follow up Question:
Or is the value of 2 actually being returned by the results variable, but there is a problem with the settingOvertakenByLength variable?
Code :
[SIZE=3]import java.util.Scanner;
import java.lang.Math;
public class FixingChap5Pro1
{
Scanner keyboard = new Scanner(System.in);
public static int compareTwoLists(String[] isThisValue, String[] onThisList, int results)
{
int b = 0;
if (isThisValue.length == 0)
{
for (int j = 0 ; j < onThisList.length ; j++)
{
b++;
}
}
else if(onThisList.length == 0);
else
{
for ( int i = 0 ; i < isThisValue.length ; i++)
{
if (compareNameToList(isThisValue[i], onThisList) == false)
b++;
}
}
System.out.println("b = " + b);
results = b;
System.out.println("resutls = " + results);
return results; //THIS VALUE IS CORRECT, BUT NOT RETURNED AND TRANSFERED
} // TO THE settingOvertakenByLength VARIABLE.
public static boolean compareNameToList( String speciesName, String[] speciesNames)
{ //HELPER METHOD NOT THE PROBLEM
boolean found = false;
for (int j = 0; j < speciesNames.length ; j++)
{
if (speciesNames[j].equals(speciesName))
found = true;
}
return found;
} //END OF HELPER METHOD NOT THE PROBLEM
public static void main(String[] args)
{
int settingOvertakenByLength = 0;
String[] firstListOfAnimalNames = {"Cat", "Dog", "Rabbit", "Republican"};
String[] secondListOfAnimalNames = {"Parrot", "Dog", "Rabbit", "Hedgehog"};
compareTwoLists(firstListOfAnimalNames, secondListOfAnimalNames, settingOvertakenByLength);
// settingOvertakenByLength VALUE SHOULD HAVE BEEN CHANGED FROM 0 BY THE ABOVE METHOD
System.out.println("settingOvertakenByLength = " + settingOvertakenByLength);
}}[/SIZE]
Thanks in advance,
SpaceMonkey:confused:
Re: Method works, value not returned.
Hello SpaceMonkey!
The compareTwoLists method returns a value but you don't assign to the settingOvertakenByLength variable. Therefore, settingOvertakenByLength has its initial value (=0). Try assigning compareTwoLists to settingOvertakenByLength.
Re: Method works, value not returned.
Dear andreas90;
Good job well done. This works.
settingOvertakenByLength = compareTwoLists(firstListOfAnimalNames, secondListOfAnimalNames, settingOvertakenByLength);
Nice solution , cheers.
Now all I have to do is mark the thread as answered and all is good.
SpaceMonkey:-bd
Cheers, again.