Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Method works, value not returned.

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Cool 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?

    [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


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    SPACE MONKEY (April 29th, 2012)

  4. #3
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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



    Cheers, again.

Similar Threads

  1. Replies: 1
    Last Post: February 2nd, 2012, 09:40 AM
  2. Help understanding this syntax -- new object returned
    By wy125 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 5th, 2011, 12:18 PM
  3. Ok, Notepad class has returned!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:50 PM
  4. Error returned
    By elie winter in forum JDBC & Databases
    Replies: 1
    Last Post: October 17th, 2010, 11:49 PM
  5. how to reuse method returned string ??
    By zeeshanmirza in forum Collections and Generics
    Replies: 7
    Last Post: September 4th, 2009, 03:54 PM