Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Call Void Method for Array

    Hello Java World!

    I am writing a program to take user input in order to create an array, then call a void method that will read in the numbers (from user's input) and fill the array.
    This method must use a loop to do this.
    (The array is to be passed to the void method as a parameter)

    In theory, this should change the contents of the array, but without returning a result.
    Because it is a void method, the array is only passed through the method, not returned; Am I correct?

    Also,
    If I did do this correctly,
    How can I return the array and display it without having to change my method type?

    Here is my code:
    Java Code:
    package program7array;
    import javax.swing.JOptionPane;
    
    public class Program7Array {
    
        public static void main(String[] args) {                                                  // main method
            int howMany = Integer.parseInt(JOptionPane.showInputDialog(null,    // user decides how long array is
                    "How many numbers are there?"));
            double [] numbersArray = new double[howMany];                             // creating the array
            makeArray(numbersArray, howMany);                                              // calling the array
            
        }
        
        public static void makeArray(double [] Array, int numbers){                  // passing array to method as parameter
            for (int i = 0; i < Array.length; i++){                                              // loop to set user input as the array length
                Array[numbers] = numbers;
            }                                                                                                  // end of loop
        }
    }
    Last edited by JosAH; 03-06-2014 at 09:13 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Call Void Method for Array

    If the return type of your method is void, it can never return anything; b.t.w. there are a few flaws in your program: if the user enters a negative number, your program barfs on it; also, the array is filled with the value 'numbers' which equals the value 'howMany' which in turn equals the value 'numbersArray.length', so the second parameter isn't necessary.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Call Void Method for Array

    Right, okay, I just wanted to verify that in my post.

    Thanks for catching that, foolish of me...
    I will go back in at a later time and write a condition for any negative values entered.

    I just realized that in my first post, within my for loop, I didn't really use the counter variable "i".

    Does this look better to you?

    package program7array;
    import javax.swing.JOptionPane;

    public class Program7Array {


    public static void main(String[] args) { // main method
    int howMany = Integer.parseInt(JOptionPane.showInputDialog(null,
    "How many numbers are there?"));
    double [] numbersArray = new double[howMany];
    makeArray(numbersArray);

    }

    public static void makeArray(double [] Array){
    for (int i = 0; i < Array.length; i++){
    Array[i] = Array.length;
    } // end of loop
    }
    }

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,275
    Rep Power
    6

    Default Re: Call Void Method for Array

    Java Code:
    Array[i] = Array.length;
    So this makes sense to you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Call Void Method for Array

    Quote Originally Posted by gimbal2 View Post
    Java Code:
    Array[i] = Array.length;
    So this makes sense to you?
    It does the same as the original version, i.e. it sets each and every element in the array to the length of the array ...

    kind regards,

    Jos

    edit: I just saw that the orignal version caused an AOOBE ... I should have more espresso ...
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Call Void Method for Array

    gimbal2,

    Not entirely, that looks like I made another error.
    Maybe it's wrong because there is no input for "i" values in Array[i]?


    public static void makeArray(double [] Array){
    for (int i = 0; i < Array.length; i++){
    Array[i]= Double.parseDouble(JOptionPane.showInputDialog(nul l, ------- Am I headed in the right direction here?
    "Enter the value."));
    } // end of loop
    }
    }

  7. #7
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Call Void Method for Array

    Jos,

    what is an AOOBE?

    Espresso is good, cleanses the soul.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Call Void Method for Array

    Quote Originally Posted by NoviceJavaNoob View Post
    Jos,

    what is an AOOBE?

    Espresso is good, cleanses the soul.
    Darn again, it should've been AIOOBE, not AOOBE; it means 'Array Index Out Of Bounds Exception'.

    Espresso is a necessity every morning (and tobacco!)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Call Void Method for Array

    Okay, thanks Jos!

    Did you happen to see my above post to gimbal2?
    I edited Array[i]=Array.length and changed it to Array[i]=Double.parseDouble(JOptionPane.showInputDialog(nu ll, "Enter the value."));

    Does this make more sense than Array[i]=Array.length?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Call Void Method for Array

    Quote Originally Posted by NoviceJavaNoob View Post
    Okay, thanks Jos!

    Did you happen to see my above post to gimbal2?
    I edited Array[i]=Array.length and changed it to Array[i]=Double.parseDouble(JOptionPane.showInputDialog(nu ll, "Enter the value."));

    Does this make more sense than Array[i]=Array.length?
    If all is well, your program now prompts for each array entry; if that is what you want, it's fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Join Date
    Feb 2014
    Posts
    15
    Rep Power
    0

    Default Re: Call Void Method for Array

    That is exactly what I wanted.

    Thanks so much for the help!


Similar Threads

  1. Replies: 3
    Last Post: 06-22-2012, 07:54 PM
  2. Replies: 2
    Last Post: 05-03-2012, 08:12 PM
  3. Replies: 3
    Last Post: 03-28-2012, 12:23 AM
  4. read or call an array from other class an method
    By ki_ha1984 in forum New To Java
    Replies: 5
    Last Post: 01-23-2012, 11:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •