Results 1 to 8 of 8
  1. #1
    Hey Taclone is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default 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;
    }

    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Please use CODE tags and format your code properly so we can read it easily.
    Quote Originally Posted by Hey Taclone View Post
    Java Code:
    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;
        	}
    OK, so we have an array of 99,999 random ints, and one lonely zero out there at array[99999].
    Quote Originally Posted by Hey Taclone View Post
    Java Code:
        	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);
    ...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.
    Quote Originally Posted by Hey Taclone View Post
    Java Code:
        }
    
        public boolean linearSearch(int key) {
            int index = 0;
            while(index < 100000) {
                if(array[index] == key) {
    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.
    Quote Originally Posted by Hey Taclone View Post
    Java Code:
                    return true;
                }
                if(array[index] < key) {
                    return false;
    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?
    Quote Originally Posted by Hey Taclone View Post
    Java Code:
                }
                index++;
            }
            return false;
        }
    
    }
    -Gary-

  3. #3
    Hey Taclone is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    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?

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Hey Taclone View Post
    Is there any way to share it from the main method?
    Yes. Pass it as a parameter.

    Arrays are objects in Java, so an array variable holds a reference to the object. When you pass an array variable as a parameter, the array is not copied, but remains where it is, and the method receives a reference to it.

    -Gary-

  5. #5
    Hey Taclone is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    But if I am using the method to search the key wouldnt the key need to be the parameter in this case?

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Hey Taclone View Post
    But if I am using the method to search the key wouldnt the key need to be the parameter in this case?
    You can have two parameters. Heck, you can have lots more than that.

    -Gary-

  7. #7
    Hey Taclone is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    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.

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Not int array, but int[] array, just like you declared it. And you will probably want to return the index where the key is located in the array.
    Java Code:
        public int linearSearch(int[] array, int searchKey) {
            ...
        }
    -Gary-

Similar Threads

  1. What went wrong. Methods problem.
    By ocean in forum New To Java
    Replies: 5
    Last Post: 11-02-2009, 06:28 PM
  2. Simple "if" statement problem....compiling error.
    By CYANiDE in forum New To Java
    Replies: 4
    Last Post: 10-14-2009, 09:56 PM
  3. problem compiling servlet
    By vivek_9912 in forum Java Servlet
    Replies: 1
    Last Post: 02-13-2009, 11:37 AM
  4. problem compiling servlets
    By boy22 in forum Java Servlet
    Replies: 1
    Last Post: 08-03-2007, 10:05 AM
  5. getting problem in compiling java program?
    By sathish04021984 in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:26 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
  •