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 3 of 3

Thread: calling up and printing an arrayHistogram

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default calling up and printing an arrayHistogram

    I'm writing a method called arrayhist that has an array of 30 random integers and returns a new histogram array that has 11 elements with this content:
    element 0 -- number of elements in the array that are <= 0
    1 -- number of elements in the array that are == 1
    2 -- number of elements in the array that are == 2
    ...
    9 -- number of elements in the array that are == 9
    10 -- number of elements in the array that are >= 1
    0
    Also, how do I get my solution to only traverse the array once?
    public static int[] arrayHist(int[] arr) {
    		int[] hist = new int[11];
     
    		for(int i = 0; i < arr.length; i++) {
    			int val = arr[i];
    			if (val <= 0) {
    				hist[0]++; 
    			} else if (val >= 10) {
    				hist[10]++;
    			} else {
    				hist[val]++; 
    			}
    		}
    		return hist;
    	}
    I'm having trouble with calling and printing the method from main.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calling up and printing an arrayHistogram

    I'm having trouble with calling and printing the method from main.
    Post the code that shows the problem or explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member didingnuriska's Avatar
    Join Date
    Apr 2013
    Posts
    21
    My Mood
    Cool
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: calling up and printing an arrayHistogram

    static method like yours "public static int[] arrayHist(int[] arr)" can be used in a dynamic method main
    you need to change the method like this public int[] arrayHist(int[] arr)
    and then you can add one method to printed like "public void printHist(int[]hist)"
    and then in the main method you need to instantiated the class to an object, then calling both method

    public static void main(String []ag){
    int arr[]=new in arr[fixedNumber];//define
    int hist[]=new int[fixedNumber];
    NewClass a= new NewClass();
    hist=a.arrayHist(arr);
    a.printHist(hist);
    }

Similar Threads

  1. CALLING ANOTHER PROGRAM
    By jeboi in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 6th, 2012, 07:33 PM
  2. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 01:32 PM
  3. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  4. [SOLVED] method calling
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 4th, 2010, 01:43 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