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.

Recommended Answers

All 2 Replies

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);
		}

Ahh.. it works, thanks so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.