1 2 Previous Next 16 Replies Latest reply: Jul 11, 2008 7:15 PM by 843785 RSS

    initializing an array question

    843785
      if i wanted to initialize elements of an array and get input from the user of how many there will be. where can i find some info on this?

      basically instead of initializing 0,1,2,3, and so on i wanted to do it in a for loop, but i have tried it a couple of different ways with no luck.

      maybe you guys can give me some insight on how i need to set this thing up or what i need to think about or where some of the reading is?

      any info would help
      thanks in advance guys
        • 1. Re: initializing an array question
          843785
          Initializing an array is the simplest thing on earth. You just assign the wanted values to the wanted index positions in the array. Please explain what stands in your way.
          • 2. Re: initializing an array question
            843785
            right, but i need to do it from user input of one number so if there is 9 elements then the user will input 9 and so on


            i keep geting this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

            in my loop.
            for(y = numRepitions; y > 0; y--)
                        {
                        
                           counters[y] = numRepitions; 
                           counters[numRepitions]--; 
                        }
            Edited by: izatt82 on Jul 11, 2008 3:59 PM
            • 3. Re: initializing an array question
              843785
              If you know how many pieces of data you are going to accept (and not any more than that), then you can easily use an array, but if you want to accept an amount of data that is not predetermined, then you are looking at a Collection like ArrayList, List, Variant etc...

              Arrays go like this if you are using primatives:
              int[5] i;
              for(int j=0; j<5; j++) i[j] = j;
              for objects:
              myObject[] i = new myObject[5];
              for(int j=0; j<5; j++){
                i[j] = new myObject();
              }
              Collections use Objects:
              ArrayList<myObject> l = new ArrayList<myObject>();
              //put as many new objects in as you want.
              i.add(new myObject());
              Look in the Java Tutorial under Arrays and Generics for a much better explanation.
              • 4. Re: initializing an array question
                843785
                try
                for(y = numRepitions - 1; y >= 0; y--)
                {
                  //....
                }
                • 5. Re: initializing an array question
                  843785
                  Encephalopathic wrote:
                  try
                  for(y = numRepitions - 1; y >= 0; y--)
                  {
                  //....
                  }
                  same error man

                  Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
                  • 6. Re: initializing an array question
                    843785
                    izatt82 wrote:
                    i keep geting this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
                    You're using an array out of its bounds. An array of size 10 has no 10'th index (they range from 0 to 9) . If you use the 10'th index position you get this exception. Don't do it.
                    • 7. Re: initializing an array question
                      843785
                      izatt82 wrote:
                      same error man
                      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
                      Then you are not initializing the array correctly before using it.
                      • 8. Re: initializing an array question
                        843785
                        well here is the code for it i thought i was intializing it correctly??
                        public static void main(String[] args) {
                                int y;
                                int x;
                                int maxValue = 0;
                                int numRepitions;
                                int [] counters = new int[maxValue];
                                Scanner keyin = new Scanner(System.in);
                                Random generator = new Random();
                                
                                    System.out.println("please enter how many random numbers you would like to generate");
                                    maxValue = keyin.nextInt();
                                    System.out.println("please eneter how many values your would like to draw for");
                                    numRepitions = keyin.nextInt();
                                
                                    for(y = numRepitions - 1; y >= 0; y--)
                                    {
                                    
                                       counters[y] = numRepitions; 
                                       counters[numRepitions]--; 
                                    }
                                
                        • 9. Re: initializing an array question
                          843785
                          see comments:
                          import java.util.Scanner;
                          
                          public class Foo3 {
                            public static void main(String[] args) {
                              int y;
                              int maxValue = 0;
                              int numRepitions;
                              int[] counters = new int[maxValue]; // *** you have an array of 0 elements here
                              Scanner keyin = new Scanner(System.in);
                          
                              System.out.println("please enter how many random numbers you would like to generate");
                              maxValue = keyin.nextInt();
                              System.out.println("please eneter how many values your would like to draw for");
                              numRepitions = keyin.nextInt();
                              
                              // *** it is likely here that you would want 
                              // *** to initialize your counters array with the appropriate value
                              // *** perhaps numRepititions?
                          
                              for (y = numRepitions - 1; y >= 0; y--) {
                          
                                counters[y] = numRepitions;  // I'm not clear on what you're trying
                                counters[numRepitions]--;    // to do here.
                              }
                            }
                          }
                          • 10. Re: initializing an array question
                            843785
                            Encephalopathic wrote:
                            izatt82 wrote:
                            same error man
                            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
                            Then you are not initializing the array correctly before using it.
                            What you suspect the OP hasn't done is instantiating the array. But if he hadn't he would have a null pointer exception which he hasn't so that's not the problem.
                            • 11. Re: initializing an array question
                              843785
                              so i tried where your first comment is to give it numRepitions as the elements, which did not work

                              so do i need to figure that out first or is initializing the array further down with numRepitions going to fix that?

                              so i was not initializing the array in the loop?
                              • 12. Re: initializing an array question
                                843785
                                izatt82 wrote:
                                so i tried where your first comment is to give it numRepitions as the elements, which did not work

                                so do i need to figure that out first or is initializing the array further down with numRepitions going to fix that?

                                so i was not initializing the array in the loop?
                                Stay calm, lean back and ask yourself: Why is the index going up to 10?

                                Put in traces. Watch the index as it goes!
                                • 13. Re: initializing an array question
                                  843785
                                  simplify your problem. Create a very simple class that does nothing but takes in an int input using the Scanner and with this create an array, simply populate the array with numbers that = the index numbers (0, 1, 2, 3, ... to the length - 1), and print out the array. If you can solve the simple problem, you'll likely be able to solve it in your code.
                                  • 14. Re: initializing an array question
                                    843785
                                    okay i think i got that figured out, but have a new question about printing. i will ask a new question

                                    thanks guys
                                    1 2 Previous Next