www.webdeveloper.com
Results 1 to 2 of 2

Thread: Help with writing a method that prints out an array

  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Help with writing a method that prints out an array

    Here's what I'm working on:

    Write a program that prompts the user for a value and creates an integer array of that size. Prompt for initial values (unordered) to populate the array. Your program must include code to prevent the user from entering a non-integer value (such as a symbol or a letter) for the size of the array as well as for any of the array elements. Write 4 methods: The first prints the values in the array, the second returns the average value of the array. The third finds the smallest element, and the fourth finds the largest element. Output the array data and the three calculations.

    I was able to write some code in my program that printed everything out the way it was supposed to look, but then I went back and re-read the instructions and realized that I need to write a separate method for that. I can't figure out how to do it and am on the verge of going on a stabbing rampage. This is due by 11:59pm tonight or I lose credit on it, and I've been slamming my head against the desk for a day and a half. Please help.

    Here's what I have:

    Code:
    import java.util.*;
    public class Assmt6 
    {
    
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner	(System.in);
    		try
    		{	System.out.print("Enter size of array: ");
    			int num = input.nextInt();
    			int[] someNumbers = new int[num];
    			
    			try
    			{
    				for(int i=0; i<someNumbers.length; i++)
    				{
    					System.out.print("Enter value #" + (i+1) + ": ");
    					someNumbers[i] = input.nextInt();
    				}	
    		//This prints out the data, but I can't figure out how to do it in a separate method.
    				System.out.println("\nYour Data is: ");
    				for(int i=0; i<someNumbers.length; i++)
    				{	
    			        System.out.println(someNumbers[i]);
    				}
    			}
    			
    			catch(InputMismatchException error)
    			{
    				System.out.println("Invalid array element -- please enter an integer value");
    			}
    		
    			
    		}
    	
    		catch(InputMismatchException error)
    		{
    			System.out.println("Invalid array size -- please enter an integer value");
    		}
    		
    		
    	}		
    }

  2. #2
    Join Date
    Nov 2010
    Posts
    24
    Quote Originally Posted by CaptainLaurie View Post
    Here's what I'm working on:

    I was able to write some code in my program that printed everything out the way it was supposed to look, but then I went back and re-read the instructions and realized that I need to write a separate method for that.
    If you all you need is to have the print function in a separate method then see below. I have used a public/static method in the main class because I'm not sure what other classes you have as part of your solution. Just pass the array to the function and have it print as normal.

    Code:
    import java.util.*;
    
    public class Assmt6 {
    
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner	(System.in);
    		try
    		{	System.out.print("Enter size of array: ");
    			int num = input.nextInt();
    			int[] someNumbers = new int[num];
    			
    			try
    			{
    				for(int i=0; i<someNumbers.length; i++)
    				{
    					System.out.print("Enter value #" + (i+1) + ": ");
    					someNumbers[i] = input.nextInt();
    				}
    				
    				print(someNumbers); // call the function declared below
    			}
    			
    			catch(InputMismatchException error)
    			{
    				System.out.println("Invalid array element -- please enter an integer value");
    			}
    				
    		}
    	
    		catch(InputMismatchException error)
    		{
    			System.out.println("Invalid array size -- please enter an integer value");
    		}
    		
    	}
    	
    	/** 
    	 * Function simply prints the array passed in from main 
    	 * @param printArray
    	 */
    	public static void print(int printArray[])
    	{
    		System.out.println("\nYour Data is: ");
    		for(int i=0; i<printArray.length; i++)
    		{	
    	        System.out.println(printArray[i]);
    		}
    		
    	}
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles