Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Tolls

Thread: Method takes array of ints and an integer. Then returns boolean if num is found?

  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Angry Method takes array of ints and an integer. Then returns boolean if num is found?

    Trying to write a code that takes an array, a number and returns this boolean if it finds a number. However, it keeps returning true! I HATE JAVA!!!!


    Java Code:
    import java.util.Scanner;
    public class Confuse
    {
    	public static void main(String[]args)
    	{
    		int[] myArray = {1,2,3,4,5,6};
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter a positive integer to test if it appears in the array: ");
    		int num = input.nextInt();
    		boolean isElement = true;
    		searchMethod(myArray, num, isElement);
    		System.out.println("Is the number " + num + " in the array?" + isElement);
    	}
    	public static boolean searchMethod(int[] myArray, int num, boolean IsElement)
    		{
    			boolean isElement = false;
    			for (int i=0; i < myArray.length && isElement == false; i++)
    			{
    				if (myArray[i] == num)
    				{
    					isElement = true;
    					break;
    				}
    				else
    					continue;
    			}
    			return isElement;
    		}
    }
    Last edited by Denis2k11; 01-31-2013 at 06:27 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Method takes array of ints and an integer. Then returns boolean if num is found?

    The basic structure of a Java class is:
    Java Code:
    public class MyClass {
       public static void main(String[]args) {
          // Put code in here
       }
    
       public static void method1() {
          // Put code in here
       }
    }
    You have:
    Java Code:
    public class Confuse {
       public static void main(String[]args) {
          // Put code in here
          public static void findNum() {
             // Put code in here
          }
       }
    }
    Your findNum method should be outside the main() method, not inside it.
    Denis2k11 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Estrella is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    16
    Rep Power
    0

    Default Re: Method takes array of ints and an integer. Then returns boolean if num is found?

    On line 10 you assign variable 'isElement' to 'true' do you ever change that value? That is, does 'isElement' ever get assigned a different value than the initial 'true'?

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2011, 02:25 AM
  2. Replies: 2
    Last Post: 06-04-2011, 11:52 PM
  3. Boolean operators on ints
    By rockgardenlove in forum New To Java
    Replies: 3
    Last Post: 02-10-2011, 04:14 AM
  4. Replies: 5
    Last Post: 10-30-2010, 12:05 AM
  5. Replies: 2
    Last Post: 10-15-2010, 05:09 AM

Posting Permissions

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