Results 1 to 6 of 6
  1. #1
    nellyman is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,302
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nellyman View Post
    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
    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:

    Java Code:
    for (int index= 0; index < array.length; index++)
       // print the index value and array[index] value
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    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

  4. #4
    nellyman is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    thanx it works

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,834
    Rep Power
    8

    Default

    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!

  6. #6
    nellyman is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. help! shapes output and array issues!
    By japgoth in forum New To Java
    Replies: 2
    Last Post: 01-25-2011, 09:49 PM
  2. Replies: 4
    Last Post: 03-20-2010, 04:17 AM
  3. Help with Array output Needed!
    By 2potatocakes in forum New To Java
    Replies: 2
    Last Post: 03-07-2009, 06:36 PM
  4. output from an array
    By @eddie.com in forum New To Java
    Replies: 5
    Last Post: 08-15-2008, 08:26 AM
  5. JSP to output Java String Array
    By irenavassilia in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-31-2008, 04:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •