The Artima Developer Community
Sponsored Link

Java Answers Forum
Linear Searches

1 reply on 1 page. Most recent reply: Feb 18, 2004 2:30 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Lynn Robertson

Posts: 4
Nickname: looloo
Registered: Dec, 2003

Linear Searches Posted: Feb 17, 2004 8:59 PM
Reply to this message Reply
Advertisement
I'm ready to pull out my hair. I'm pretty new to this so it's probably something really simple. Can anyone tell me why I can't enter any key larger than 10? I get the out of bounds message. What's suppose to happen is I enter an index and the search displays the integer at that index. Here's the part of the code where I know the problem exists.

Thanks,
Lynn

//Main Method
public static void main(String[] args)
{
ArrayDemo frame = new ArrayDemo();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(230, 120);
}

public int linearSearch(int key, int[] list)
{
key = Integer.parseInt(jtfIndex.getText());
list = new int[100];
for(int counter = 0;counter < list.length;counter++)
list[counter] = (int)(Math.random() * 10);

for(int counter = 0; counter < list.length; counter++)

if (key == list[counter])
return counter;
return -1;
}

//The nitty gritty
public void actionPerformed(ActionEvent e)
{
int key = Integer.parseInt(jtfIndex.getText());
int result = linearSearch(key,list);

if (e.getSource() == jbtShow)
if(result != -1)
jtfElement.setText(String.valueOf(result));
else
jtfElement.setText("Out of Bounds");

}


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Linear Searches Posted: Feb 18, 2004 2:30 AM
Reply to this message Reply
first thing first...
in the code
public int linearSearch(int key, int[] list)
{
key = Integer.parseInt(jtfIndex.getText());
list = new int[100];
for(int counter = 0;counter < list.length;counter++)
list[counter] = (int)(Math.random() * 10);
 
for(int counter = 0; counter < list.length; counter++)
 
if (key == list[counter])
return counter;
return -1;
}


why are u passing key and list as parameters when u are again initializing it in the function?? Does passing those values serve any purpose ?? No...

Now about the error..
I tried the method with any values as key in the method as i dnt have the frame/textfield and all comp and it worked fine.

May be u are getting error at some otherplace...

Flat View: This topic has 1 reply on 1 page
Topic: Parameter error! Previous Topic   Next Topic Topic: Drawing Method Problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use