Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic Java Array Question

Options
  • 20-11-2007 1:38pm
    #1
    Registered Users Posts: 54 ✭✭


    Ok, very new to Java. The goal here was to read in 10 integers into an array, then print the array elements out in reverse order.

    Can't seem to get it to work though.

    I guess my problem is with the second "for" loop here, not with the array really.

    Any help much appreciated, and I bet it will take about a second for someone to point out what I've done wrong. :o

    public class MyArray
    {
    public static void main(String[] args)
    {

    int numbers[10]; //declare array "numbers" which will hold 10 integers


    //create array and assign its address to the variable "numbers"

    numbers = new int[10];

    //read in integers

    System.out.println("Please enter first integer:");

    for (int i=0; i<numbers.length; i++)
    { numbers = Console.readInt();
    System.out.println("Please enter next integer:");
    }

    //print out array elements in reverse

    for (int i=9; i<=numbers.length; i--)
    {
    System.out.println(" " + numbers + " ");
    }
    }
    }


Comments

  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Well I don't know Java, but on the 2nd for loop, since you're deincrementing, the exiting condition should be when the counter hits zero... not when it's less or equal to the size of the array.

    And surely there's some array function you can use to re-order the array instead of just counting back though it?


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


          for (int i=numbers.length-1; i>0; i--)
          {  
             System.out.println(" " + numbers[i] + " ");
          }
    

    Just change the second for loop to this.


  • Registered Users Posts: 5,580 ✭✭✭veryangryman


    //Your welcome! Go in peace my son

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;


    public class MyArray
    {
    public static void main(String[] args)
    {

    int numbers[]; //declare array "numbers" which will hold 10 integers


    //create array and assign its address to the variable "numbers"

    numbers = new int[10];

    //read in integers
    int x=1;

    System.out.println("Please enter first integer:");

    for (int i=0; i<numbers.length; i++)
    {
    numbers = x;
    System.out.println(" " + numbers + " ");
    x+=1;


    }

    //print out array elements in reverse

    for (int i=9; (i<=numbers.length) && (i>0); i--)
    {
    System.out.println(" " + numbers + " ");
    }
    }
    }


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    That second loop may never execute if there are 8 elements in the array, and there's a bunch of imports you don't need...
    for (int i=numbers.length-1; i>=0; i--)
    {  
         System.out.println(" " + numbers[i] + " ");
    }
    


  • Registered Users Posts: 54 ✭✭JonT


    Thanks all!
    Giblet, yours is the most correct code, is it not?

    since

    for (int i=numbers.length-1; i>=0; i--)
    {
    System.out.println(" " + numbers + " ");
    }

    will also print out the array element at position 0, where as

    for (int i=numbers.length-1; i>0; i--)
    {
    System.out.println(" " + numbers + " ");
    }

    would only print out those at positions 9 to 1.

    Am I right?

    Thanks again people!


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Yep my bad. Should have been i >=0 :o


Advertisement