Newbie unable to printarray of array components.

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

Sdot718Asked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:

this will print somthing
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);
		}
			public static void printArray(double[]alpha)
			{
				for( int index = 0; index < alpha.length; index++)
				{
					//if(index< 15)
					alpha[index] = index * index;
			System.out.print(alpha[index] + " ");
                    if(((index+1)%10)==0)System.out.println("");
		}
}
}

Open in new window

0
 
ksivananthCommented:
first you need to call printArray from main method!
0
 
CEHJCommented:
You're not calling printArray at all at the moment ...
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Sathish David Kumar NArchitectCommented:
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]);  
                }  
0
 
CEHJCommented:
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

0
 
Sathish David Kumar NArchitectCommented:
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

0
 
Sathish David Kumar NArchitectCommented:
  sorry CEHJ: i didnt see ur command ..


 printArray(alpha[]); ------------->remove
0
 
Sathish David Kumar NArchitectCommented:
BufferedReader reader = new BufferedReader(System.in);
String input = reader.readLine();

use this code to get input from console ....
0
 
CEHJCommented:
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

0
 
Sdot718Author Commented:
Fast and the program did what I asked.
0
 
Sathish David Kumar NArchitectCommented:
@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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.