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 8 of 8

Thread: error: This method must return a result of type int

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default error: This method must return a result of type int

    Hello, I am getting this error with my code "error: This method must return a result of type int" haven't a clue why it's happening.

    int processresponse(String response){
    int a;
    Boolean foundresponse = false;
    	for (a = 0; a < vresponses.size(); a++)
    	{ // Check if response exists.
    		if (vresponses.get(a) == response)
    		{
    			foundresponse = true;
    			return a;
    		}
    	}
     
    	if (foundresponse == false)
    	{
    		vresponses.add(response);
    		return vresponses.size()-1;
    	}
    }

    Any help much appreciated.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: error: This method must return a result of type int

    public int processResponse(String response) {
            int a;
     
            boolean foundResponse = false;
            for (a = 0; a < vresponses.size(); a++) { // Check if response exists.
                if (vresponses.get(a) == response) {
                    foundResponse = true;
                    return a;
                }
            }
     
            if (!foundResponse) {
                vresponses.add(response);
                return (vresponses.size() - 1); 
            }
            return (vresponses.size() - 1); //Must return Integer here.. 
        }

    You were missing a return statement as you were only returning values if certain conditions were true, and your method always needs an Integer to be returned.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: error: This method must return a result of type int

    Thanks...

    So is "(!foundResponse)" the same as "(foundResopnse == false)"?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: error: This method must return a result of type int

    Yeah it is.
    Id say its better convention than ==false/true.

    The same goes for true.

    if (isTrue) {
                //do something if isTrue == true
            } else if (!isTrue) {
                //do something if isTrue == false}
            }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: error: This method must return a result of type int

    Also, I think you're looking for
    if (vresponses.get(a).equals( response))
    		{
    			foundresponse = true;
    			return a;
    		}

    Actually, why it was giving that error message, I'm not quite sure. If it wasn't found, it would return the new index. If it was, it would return that index.

    The only thing I can think of is that it wanted a () around your second return statement.

    Actually that would sorta make sense.

    Otherwise it might be confused on what to return

    For instance

    (int) Math.random() * 5
    Would be returning an int of Math.random(), not an int of Math.random() * 5.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: error: This method must return a result of type int

    Also, I might add that there's no need to use the Boolean wrapper class. I think that's only if you needed to either store the Boolean in like a Vector, ArrayList, Set, Map, Tree, or Linked List, or some other type of Collection. Or if you needed to make it turn into a String, like for a TextField, textArea, etc. I can't think of any other uses other than those two I mentioned above. (However, perhaps, I never came across this yet, it might possibly be to use the equals(Object obj) method to check to see if two booleans were equal to each other. However, I believe boolean1 == boolean2 is also valid and preferable. ) You should probably use the primitive boolean.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: error: This method must return a result of type int

    Actually newbie is quite correct.

    Java doesn't expect you to make needless checks, so it assumes that if blocks won't necessarily execute.

  8. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: error: This method must return a result of type int

    If it finds it, foundResponse is set to true, but that doesn't matter as the return statement will return a and exit the method if it finds it. If it never gets set to true, it'll go into the if loop happens if it can't find it. The third return statement doesn't make much sense.

    Also, it should be .equals(response).

Similar Threads

  1. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  2. where is the error in this method !
    By M7MD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2010, 05:08 PM
  3. Return result from JOptionPane to JFrame
    By cselic in forum AWT / Java Swing
    Replies: 7
    Last Post: May 13th, 2010, 01:17 PM
  4. Replies: 4
    Last Post: June 18th, 2009, 09:23 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM