We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Newbie unable to printarray of array components.

Sdot718
Sdot718 asked
on
Medium Priority
281 Views
Last Modified: 2012-05-11
Hello very new to java and would like some feedback.
I am trying to print the array for my program so that 10 array components print out every line.

When I run it the program only shows press any key to continue.
Any help would be thank full.
import java.util.*;

public class PrintA
{
	public static double[] alpha = new double[30];
	public static void main(String[] args)
	{

		for(int index = 0; index < alpha.length; index++)
		{

		if(index < 15)
		alpha[index] = index * index;
			else
			{
				alpha[index] = index * 3;
			}

			}

		}
			public static void printArray(double[]alpha, int index)
			{
				for( index = 0; index < alpha.length; index++)
				{
					if(index< 15)
					alpha[index] = index * index;
			System.out.println(alpha[index]);
		}
}
}

Open in new window

Comment
Watch Question

ksivananthVice President

Commented:
first you need to call printArray from main method!
CERTIFIED EXPERT
Top Expert 2016

Commented:
You're not calling printArray at all at the moment ...
CERTIFIED EXPERT

Commented:
and why ur passing index varibale ...

no need to pass

  public static void printArray(double[]alpha)  
                        {  
                                for(int  index = 0; index < alpha.length; index++)  
                                {  
                                        if(index< 15)  
                                        alpha[index] = index * index;  
                        System.out.println(alpha[index]);  
                }  
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
Not sure exactly what you mean, but try
import java.util.*;


public class PrintA {
    public static double[] alpha = new double[30];

    public static void main(String[] args) {
        for (int index = 0; index < alpha.length; index++) {
            if (index < 15) {
                alpha[index] = index * index;
            } else {
                alpha[index] = index * 3;
            }

	    printArray(alpha, index);
	    System.out.println();	
	}
    }

    public static void printArray(double[] alpha, int index) {
	for (index = 0; index < alpha.length; index++) {
	    if (index < 15) {
		alpha[index] = index * index;
		System.out.printf("%.0f ",alpha[index]);
	    }

	}
    }
}

Open in new window

CERTIFIED EXPERT

Commented:
import java.util.*;  
  
public class PrintA  
{  
        public static double[] alpha = new double[30];  
        public static void main(String[] args)  
        {  
  
                for(int index = 0; index < alpha.length; index++)  
                {  
  
                if(index < 15)  
                alpha[index] = index * index;  
                        else  
                        {  
                                alpha[index] = index * 3;  
                        }  
  
                        }  
                         [b]printArray(alpha[]);[/b]                }  
                        public static void printArray(double[]alpha)  
                        {  
                                for[b](int  index = 0[/b]; index < alpha.length; index++)  
                                {  
                                        if(index< 15)  
                                        alpha[index] = index * index;  
                        System.out.println(alpha[index]);  
                }  
}  
}

Open in new window

CERTIFIED EXPERT

Commented:
  sorry CEHJ: i didnt see ur command ..


 printArray(alpha[]); ------------->remove
CERTIFIED EXPERT

Commented:
BufferedReader reader = new BufferedReader(System.in);
String input = reader.readLine();

use this code to get input from console ....
CERTIFIED EXPERT
Top Expert 2016

Commented:
I think your print method should be more like
public static void printArray(double[] alpha) {
	for (index = 0; index < 10; index++) {
		System.out.printf("%.0f ",alpha[index]);
	}
    }

Open in new window

Author

Commented:
Fast and the program did what I asked.
CERTIFIED EXPERT

Commented:
@Sdot718:

CHEJ and me only tell the answer before for_yan the same thing only he given.... This is not far at all ..

Ur beginner soo try to write your own code .. get outline(guide) only
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.