Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Printing an array.

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Printing an array.

    I am having trouble trying to call printArray in main. I want to output (array) in the method printArray.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package arrayhistogram;
     
    /**
     *
     * @author Logan
     */
    public class ArrayHistogram {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
        }
        public static int arrayHistogram (int low, int high) {
     
        low = 2;
        high = 78;  
        int random = (int)(Math.random()*100);
        return random;
    }
        public static int[] randomIntArray (int n) {
     
            int[] a = new int[n];
            for (int i = 0; i<a.length; i++) {
                a[i] = arrayHistogram (-5, 15);  
            }
            return a;
            }
        public static void printArray (int[] a){
            for (int i = 0; i<a.length; i++) {
                System.out.println(a[i]);
                int numValues = 8;
                int[] array = randomIntArray (numValues);
                printArray (array);
            }
        }
     
        }


    --- Update ---

    In main i have tried to printArray (array); but it doesn't work, cannot find symbol ;s


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    24
    My Mood
    Angelic
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Printing an array.

    MAIN is Empty!!

    Try this way:

    public static void main(String[] args) {
    int numValues = 8;
    int[] array = randomIntArray (numValues);
    printArray (array);
    }


    What troubles you have in calling the method in the Main??

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Printing an array.

    Logan, I have tried running your program in my IDE. Here are some remarks:

    REMARK 1: You need to invoke printArray(array); in main() -- I see that the main() method in the code you posted is empty. You will need to change your main() method to something like the following:

    public static void main(String[] args) 
    {
        	// initialize some array a[] here
            // invoke the method printArray()
        	printArray(a);
    }

    REMARK 2: It isn't quite clear to me what you are trying to achieve, but you have a recursive call in your method printArray(). If you are unfamiliar with the term "recursive," in this context, it refers to a method calling itself. You are calling printArray(array) inside of your method printArray() -- this will result in a stack overflow.

    REMARK 3: Unless you provide more information, I cannot be sure of what you are trying to achieve, but from looking at it, here is my guess: you have a method int [] randomIntArray(int n), and you will use to method to generate some array. You will then use the array generated by randomIntArray() as an argument for printArray(). If my guess is correct, the following code should do the trick for you:

    /****************/

    Hope that helps. Good luck.
    Last edited by jps; November 15th, 2012 at 04:29 AM. Reason: spoonfeeding

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Printing an array.

    @vluong please read "The problem with spoonfeeding"

Similar Threads

  1. Printing 2d array
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 29th, 2012, 08:41 PM
  2. Help printing from an array using outFile
    By kcarls22 in forum Collections and Generics
    Replies: 6
    Last Post: November 21st, 2011, 09:37 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. Printing an array
    By native in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 16th, 2011, 09:07 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM