Results 1 to 8 of 8
- 05-06-2010, 04:08 AM #1
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Having a Problem Compiling with 2 methods
I cant figure out how to get the compiler to acccept my new method that I want to use in my first method. It isn't remembering my array that I created in the first method. If you could help me that would be awesome :)
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class project10 {
public static void main(String [] args) {
System.out.println("Perezjm");
int[] array = new int [100000];
int a;
int b;
int c;
int d;
int e;
for(int n = 0; n < 99999; n++){
int randomNumber = (int)(Math.random() * 100000);
array[n] = randomNumber;
}
a = (int)(Math.random() * 100000);
b = (int)(Math.random() * 100000);
c = (int)(Math.random() * 100000);
d = (int)(Math.random() * 100000);
e = (int)(Math.random() * 100000);
}
public boolean linearSearch(int key) {
int index = 0;
while(index < 100000) {
if(array[index] == key) {
return true;
}
if(array[index] < key) {
return false;
}
index++;
}
return false;
}
}
- 05-06-2010, 04:30 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Please use CODE tags and format your code properly so we can read it easily.
OK, so we have an array of 99,999 random ints, and one lonely zero out there at array[99999].
...and five more random ints that we just throw away, because main() is ending here and we haven't done anything with them. Our array gets thrown away too, because it was declared in main(). So all that we've done of any consequence is to print "Perezjm" to the console.
We don't have any valid array variable here, so we'll get a compile error. That array variable either needs to be an instance variable of the class, or needs to be passed as a parameter to this method.
This looks like a very bad idea unless we somehow know that our array is sorted in descending order. In any case, do we really only want to know that key is somewhere in the array? We don't need to know where?
-Gary-
- 05-06-2010, 04:58 AM #3
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Yea, the code isn't complete. I was kinda of finishing the total project in parts. I really appreciate the insight and sorry it wasnt in the proper format. I have it like that when I write it but it didnt paste back that way :/ You answered the question I had though. I couldnt figure out why it wouldnt accept the previously declared array but I guess it has to either be the parameter or in the method. Is there any way to share it from the main method?
- 05-06-2010, 05:05 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 05-06-2010, 05:09 AM #5
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
But if I am using the method to search the key wouldnt the key need to be the parameter in this case?
- 05-06-2010, 05:12 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 05-06-2010, 05:29 AM #7
Member
- Join Date
- May 2010
- Posts
- 4
- Rep Power
- 0
Ok one more question and I think i should be good. How would I word it. Would it be:
public static boolean linearSearch(int key, int array)
And thank you so much for all the help.
- 05-06-2010, 08:58 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
What went wrong. Methods problem.
By ocean in forum New To JavaReplies: 5Last Post: 11-02-2009, 06:28 PM -
Simple "if" statement problem....compiling error.
By CYANiDE in forum New To JavaReplies: 4Last Post: 10-14-2009, 09:56 PM -
problem compiling servlet
By vivek_9912 in forum Java ServletReplies: 1Last Post: 02-13-2009, 11:37 AM -
problem compiling servlets
By boy22 in forum Java ServletReplies: 1Last Post: 08-03-2007, 10:05 AM -
getting problem in compiling java program?
By sathish04021984 in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks