• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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

0
Sdot718
Asked:
Sdot718
1 Solution
 
ksivananthCommented:
first you need to call printArray from main method!
0
 
CEHJCommented:
You're not calling printArray at all at the moment ...
0
 
Sathish David Kumar NCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:

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
 
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 NCommented:
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 NCommented:
  sorry CEHJ: i didnt see ur command ..


 printArray(alpha[]); ------------->remove
0
 
Sathish David Kumar NCommented:
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 NCommented:
@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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now