Link to home
Start Free TrialLog in
Avatar of Sdot718
Sdot718

asked on

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

Avatar of ksivananth
ksivananth
Flag of United States of America image

first you need to call printArray from main method!
Avatar of CEHJ
You're not calling printArray at all at the moment ...
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]);  
                }  
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
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
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

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

  sorry CEHJ: i didnt see ur command ..


 printArray(alpha[]); ------------->remove
BufferedReader reader = new BufferedReader(System.in);
String input = reader.readLine();

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

Avatar of Sdot718
Sdot718

ASKER

Fast and the program did what I asked.
@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