Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phatcyndy
    New Member
    • May 2008
    • 2

    #1

    Arrays

    Hi
    how do i print array contents my code is as follows

    import java.util.*;

    public class Question1
    {
    private int[]numbers=new int[19];
    private int option;
    private int MAX=20;
    private int num;
    private int count;
    public static void main(String[]args)
    {
    Question1 a= new Question1();
    a.menu();
    }
    public void menu()
    {

    while(option!=7 )
    {
    System.out.prin tln(" Please select an option");
    System.out.prin tln("1. to store number");
    System.out.prin tln("2. to print ");
    System.out.prin tln("3. to search ");
    System.out.prin tln("4. to remove ");
    System.out.prin tln("5. to set ");
    System.out.prin tln("6. to calculate average ");
    System.out.prin tln("7. to quit ");
    System.out.prin tln(" option: ");
    Scanner scan=new Scanner(System. in);
    option=scan.nex tInt();
    if (option==7)
    {
    System.exit(0);
    }else if (option==1)
    {
    enterNumber();
    }

    else if (option==2)
    {
    printNumbers();
    }
    //else if(option==3)
    // {
    // a.searchNumber( );
    // }
    // else if(option==4)
    // {
    // // a.removeNumber( );
    // }else if (option==5)
    // {
    // // a.setNumber
    // }else if(option==6)
    // {
    // // a.calcuate();
    // }


    }

    }

    public void enterNumber()
    {
    while (count!= MAX)
    {
    System.out.prin tln("Enter intergers between 1 and 20 or (99 to quit)");
    Scanner scan=new Scanner(System. in);
    int num=scan.nextIn t();
    count++ ;
    if (num==99)
    {
    System.out.prin tln("Program quit");
    System.exit(0);
    }else if(numbers .length >MAX)
    {
    System.err.prin tln("Too much data!!!");
    System.exit(0);
    }

    }
    }
    // having trouble here
    public void printNumbers()
    {
    for(int a =1;a<=MAX;a++)
    {
    if(num>0)
    {
    System.out.prin tln (numbers[a +1]);
    }
    return 0;
    }
    }


    // public void searchNumber()
    //
    // {
    //
    // System.out.prin tln("Please enter a specified number ");
    // Scanner scan= new Scanner(System. in);
    // int num1=scan.nextI nt();
    //
    // if (numbers[num1]== num1)
    // {
    // System.out.prin tln("The number " + num1 + " is found ");
    // return num1;
    //
    // }else
    // System.out.prin tln("The number " + num1 +" is not found");
    // return -1;
    // }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You array indexing technique is goofy: you have an array with 19 elements (see
    your own code). Array indexes start at zero (0) in Java so the valid index values
    are 0, 1, 2, 3 ... 17, 18. Your code will cause an IndexOutOfBound sException
    at several places.

    You can print the contents of an array in an extremely lazy way: check the
    methods in the Arrays utility class. You don't even need to craft a loop
    for it.

    kind regards,

    Jos

    Comment

    Working...