import java.util.*;
import javax.swing.JOptionPane;
public class ArrayPractice
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int[] values1 = new int[10];
int[] values2 = new int[10];
inputArray(values1);
doubleArray(values1, values2);
printArray(values1);
printArray(values2);
}
public static void inputArray(int[] values)
{
int i;
String valueStr = " ";
for (i = 0; i < values.length; i++)
valueStr = JOptionPane.showInputDialog
("Entering 10 integers ... value " + i);
values = Integer.parseInt(valueStr);
}
public static void doubleArray(int[] v1, int[] v2)
{
int i;
int doubled;
for (i = 0; i < v1.length; i++)
v2 = v1*2;
}
public static void printArray(int[] value)
{
String outputStr;
outputStr = "The value in the array are: \n\n" +
String.format( value[0]+ " " + value[1]+ " " + value[2] ) + "\n" +
String.format( value[3]+ " " + value[4]+ " " + value[5] ) + "\n" +
String.format( value[6]+ " " + value[7]+ " " + value[8] ) + "\n" +
String.format( value[9]+ " " );
JOptionPane.showMessageDialog(null, outputStr, "Array Practice", JOptionPane.INFORMATION_MESSAGE);
}
}
need help in a java program!
I have a school assignment and im coming up with an error
my source code is
its coming up with the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at ArrayPractice.inputArray(ArrayPractice.java:31)
at ArrayPractice.main(ArrayPractice.java:13)
However this error comes up only when i execute. It has a clean compile.
if anyone knows I appreciate it
try this
In your code the assignment is executed only when the loop ends. When it happens, i is equal to 10 (too much, since the array range is 0-9). With the parentesis you tell the compiler to execute both lines at each iteration.
public static void inputArray(int[] values) { int i; String valueStr = " "; for (i = 0; i < values.length; i++){ valueStr = JOptionPane.showInputDialog ("Entering 10 integers ... value " + i); values = Integer.parseInt(valueStr);} }
In your code the assignment is executed only when the loop ends. When it happens, i is equal to 10 (too much, since the array range is 0-9). With the parentesis you tell the compiler to execute both lines at each iteration.
Very simple, actually. In the inputArray(int[] values) function you had forgoten to put the two statments into brackets for the for loop. Therefor the compiler searches for a none existing variable i.
If there is a nother problem, I haven't checked.
If there is a nother problem, I haven't checked.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement