Link to home
Start Free TrialLog in
Avatar of H. G.
H. G.

asked on

Java help

Ok, I need to write a program in a class named "whatever" and ignore the main method for a moment.

In this class, I need to create a static method with one parameter of type int[ ] and Inside the method
a. Keep all output on one line I guess using System.out.print( )
b. Display an opening square bracket character.
c. Loop through the array that was passed into the method, display the values in the array, add a comma
and a space after every value except the last one.
d. then display a closing square bracket character...I guess using System.out.println().

Then, in my main method I've got to create an array and pass the reference to this array to the "printArray" method.
    int[ ] oneD = {5, 6, 7, 8};
Then in my main method, I need to add a blank println() statement after the method call just made.
and in the class, create an overloaded static method  with one parameter of type int[ ] [] named "arr" and inside:
a. Use println() to display an opening square bracket character.
b. Loop through the two-­dimensional array that was passed into the method.
     i. First, use System.out.print() to display two space characters.
     ii. Every element of this two­-dimensional array that you are looping through is a one­-dimensional
         array of int. Call the other printArray method and pass to it each one­-dimensional array in the
         two­-dimensional array.
c. Using println(), display a closing square bracket character.

Then in the main method, I need to create the following two­-dimensional array and pass the reference to this array to the printArray method.
int[ ][ ] twoD = {{2, 4, 6, 8}, {8, 7, 9, 1},
{3, 5, 1, 2}};

In the main method, add a blank println() statement after the method call you just made.

In the main method, create the following ragged two-­dimensional array. Pass the reference to this array to the
printArray method.


       int[ ][ ] twoD2 = {{1, 2}, {3, 4, 5}, {6}, {7, 8, 9}};

I think the results (output) should look identical to this
       /* Results:
       [5, 6, 7, 8]
       [
       [2, 4, 6, 8]
       [8, 7, 9, 1]
       [3, 5, 1, 2]
       ]
       [
       [1, 2]
       [3, 4, 5]
       [6]
       [7, 8, 9]
       ]
       */
Any help would be appreciated.  Thanks!
Avatar of H. G.
H. G.

ASKER

Here's what I have thus far...
public class ArrayPrinter {

	
	public static void main(String[] args) {
		

		        int[] oneD = {5, 6, 7, 8 };  
                     
		        int[][] twoD = {{2,4,6,8},{8,7,9,1},{3,5,1,2}}; 

		        int[][] twoD2 = {{1,2},{3,4,5},{6},{7,8,9}};

		        
		        printArray(oneD);
		        printArray(twoD);
                printArray(twoD2);
		     
		    }

		    
	
	        public static void printArray(int[] arr) 

		    {
	        	System.out.println("[");
	        	 for (int i = 0; i < arr.length; i++)
		       {

		    	   
		        	 if (i == arr.length - 1)
		        		System.out.print(arr[i]);

		        	else
                        System.out.print(arr[i] + ",");
	 
		        }
                     System.out.println("]");
		       
		    }

		    

		    public static void printArray(int[] [] arr) 

		    {

		    	
               System.out.println("[");
               for (int i = 0; i < arr.length; i++)
		        	printArray(arr[i]);
		                System.out.println("]");
		        

		    }

Open in new window

Avatar of CEHJ
What is your question?
I agree with CEHJ.  
The code you posted is almost there. I didn't have to change much at all to get the output you want to print.
Avatar of H. G.

ASKER

I'm not getting the desired output. What am I missing?  I've been racking my brain with this
All I did was change line 25  from println  to print and add a space character after the comma on line 34, and add a } to the end.
Avatar of H. G.

ASKER

so rrz, line 25
System.out.println("[");

Open in new window

should say
System.out.print("[");

Open in new window

and 34
System.out.print(arr[i] + ",");

Open in new window

should be
System.out.print(arr[i] + ", ");

Open in new window

and 54 should be
}}

Open in new window


Is that what you suggest?
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of H. G.

ASKER

Awesome!  Thanks.  

Not DO school work, HELP.  

Thanks again.