Jump to content

Linear search help plz

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
mikesta

mikesta

    Newbie

  • Members
  • Pip
  • 4 posts
This Linear search thing wont work helpppppppppppppppppppppppzzzzzzzzzzzzzz plzzzzzzzzzzzzzzzzzzzzzzzzzz ....it gives out an error message on
//////////int[] a = ArrayUtil.randomIntArray(20, 100); //random numbers
and
/////////////////////LinearSearcher searcher = new LinearSearcher(a);
package javaapplication22;

import java.util.Arrays;
import java.util.Scanner;


public class LinearSearchDemo {
public static void main(String[] args) {
int[] a = ArrayUtil.randomIntArray(20, 100); //random numbers

System.out.println(Arrays.toString(a)); 
LinearSearcher searcher = new LinearSearcher(a);
Scanner in = new Scanner(System.in);

boolean done = false;
while (!done)
{
System.out.println("Enter Number to search for, -1 to quit");
int n = in.nextInt();
if (n==-1)
done = true;
else
{
int pos = searcher.search(n);
System.out.println("Found in position" + pos);
}
}
}
}

Edited by Orjan, 10 December 2009 - 02:23 PM.
Use code tags when posting code


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Easiest way to do a linear search must be manually...

int i = 0;
while (i < a.length() && a[i++] != n);
if (i == a.length) {
  // could not be found
} else {
  // found on place i
}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It would seem the reason you're having problems is you don't have an ArrayUtil or LinearSearcher object defined. Neither of those are part of the Java standard, and if they're programmed in your package somewhere, it's not picking them up. If you don't have them in the same package this is being programmed in, you'll need to import them.
Wow I changed my sig!

#4
mikesta

mikesta

    Newbie

  • Members
  • Pip
  • 4 posts
thx guys appreciate ur help!!!!!