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

Thread: Linear Search in an array

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Linear Search in an array

    I'm trying to run this code to find the key in the array
    If the element is not 5 it is supposed to return a -1
    The code runs but doesn't return anything. What am I doing wrong?

    public class Search{

    public static void main(String[] args){

    int[] list = new int [5];
    list[0] = 1;
    list[1] = 2;
    list[2] = 3;
    list[3] = 4;
    list[4] = 5;

    int key = 5;

    linearSearch(list, key);
    }

    public static int linearSearch(int [] list, int key) {
    for(int i = 0; i < list.length; i ++){
    if (key == list [i])
    return i;
    }
    return -1;

    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Linear Search in an array

    Please read the Announcement topic at the top of the sub-Forum to learn how to post code correctly and ask good questions.

    You don't do anything with the value that is returned. You can print it, assign it to a value, etc. Right now, the method completes, returns a value, and nothing is done with it.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Linear Search in an array

    How do I return the - 1 to show in the main?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Linear Search in an array

    Replace the last line in the main() method with:

    System.out.println( linearSearch(list, key) );

Similar Threads

  1. How To Use Linear Search
    By Semion in forum Collections and Generics
    Replies: 17
    Last Post: May 13th, 2013, 12:40 AM
  2. Linear Search / Index Location of Strings
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 07:11 AM
  3. Code for Linear & Binary Search using ArrayLists
    By JavaBeginner12 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 11th, 2012, 09:22 AM
  4. [SOLVED] Linear array search(without Arraylist)
    By Usoda in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2011, 01:12 AM
  5. [SOLVED] Help with array of Strings...Linear Search?
    By Stockholm Syndrome in forum Collections and Generics
    Replies: 4
    Last Post: March 22nd, 2011, 03:31 PM

Tags for this Thread