Results 1 to 6 of 6
Thread: problem with output from Array
- 01-25-2011, 04:20 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
problem with output from Array
Hi all
my code gives wrong output
this is the snippet of code i am using
class ForDemo {
public static void main(String[] args) {
int[] numbers = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
for (int item : numbers)
for (int x = 0; x < numbers.length; x++)
{
System.out.println("Element at index " + x + " is " + item);
}
}
}
i need it to ouput
Element at index 0: is 100
Element at index 1: is 200
etc
below is the output I am getting
- 01-25-2011, 04:28 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
You don't need a nested loop for a one dimensional array; one loop will do fine. Because you also want to print the index an enhanced for-loop is not so handy. Simply use an old fashioned loop:
kind regards,Java Code:for (int index= 0; index < array.length; index++) // print the index value and array[index] value
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-25-2011, 04:29 PM #3
Hi
Java Code:public static void main(String[] args) { int[] numbers = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}; for (int item=0;item!=numbers.length;++item) { System.out.println("Element at index " + item + " is " + numbers[item]); } }Skype: petrarsentev
http://TrackStudio.com
- 01-25-2011, 05:25 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
thanx it works
- 01-25-2011, 05:29 PM #5
Hooray spoonfeeding :-/
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-25-2011, 05:41 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
help! shapes output and array issues!
By japgoth in forum New To JavaReplies: 2Last Post: 01-25-2011, 09:49 PM -
How can I get my array output result onto one dialog box???
By Reero8532 in forum New To JavaReplies: 4Last Post: 03-20-2010, 04:17 AM -
Help with Array output Needed!
By 2potatocakes in forum New To JavaReplies: 2Last Post: 03-07-2009, 06:36 PM -
output from an array
By @eddie.com in forum New To JavaReplies: 5Last Post: 08-15-2008, 08:26 AM -
JSP to output Java String Array
By irenavassilia in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-31-2008, 04:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks