Jump to content
  • Advertisement
Sign in to follow this  
l jsym l

need help in a java program!

This topic is 3130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a school assignment and im coming up with an error my source code is
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);		
	}
		
	
}
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

Share this post


Link to post
Share on other sites
Advertisement
try this


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!