Results 1 to 3 of 3
  1. #1
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Returning a value instead of false for a boolean

    I would appreciate if anyone could help me with what seems to be a very simple question but i cannot get it to work. I have the following method:

    public boolean search (int parameter)

    {

    for (int i = 0; i < intArray.length; i++)
    {
    if (parameter >= 0 && parameter <= i){
    return true;
    }

    }
    return false;


    }

    However instead of returning false i would like to return -1. Java does not compile as it expects a boolean. I know this is a basic question but can anyone please help.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Returning a value instead of false for a boolean

    In that case you will need to change the return type of the method to int and have both return statements changed. You CANNOT have a method return two different types: true or -1.

  3. #3
    NewToJava_99 is offline Member
    Join Date
    Nov 2011
    Posts
    7
    Rep Power
    0

    Default Re: Returning a value instead of false for a boolean

    Thank you Junky, that was it. I read over that peace of code so many times and still missed it. Talk about missing the obvious.

Similar Threads

  1. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  2. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  3. Set TexField to false
    By globo in forum New To Java
    Replies: 3
    Last Post: 11-18-2010, 08:46 PM
  4. can't get setVisible(false) to work
    By alexander.s in forum AWT / Swing
    Replies: 0
    Last Post: 04-27-2010, 08:33 PM
  5. swirling becomes false,why?
    By arefeh in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 06:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •