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.
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]);
}
}
}
first you need to call printArray from main method!
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[i ndex]);
}
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[i
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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]);
}
}
}
}
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]);
}
}
}
sorry CEHJ: i didnt see ur command ..
printArray(alpha[]); ------------->remove
printArray(alpha[]); ------------->remove
BufferedReader reader = new BufferedReader(System.in);
String input = reader.readLine();
use this code to get input from console ....
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]);
}
}
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
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