0

Hi everyone,

I'm having a problem with understanding the workings of an enhanced For loop. This code is meant to iterate through an array of ints and print out the char value of that int. But it seems to start on the first number in the array, making the program throw an ArrayIndexOutOfBoundsException.

final int[] message = {60, 80, 100...}
		for(int i: message)
		{
			System.out.print((char)message[i]);
		}

any help would be great thanks.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Danny_501
0

You're misunderstanding the concept of a for each loop. Change the code to this:

final int[] message = {60, 80, 100...}
		for(int i: message)
		{
			System.out.print((char)i);
		}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.