12 Replies Latest reply: Apr 14, 2008 9:55 PM by 807601 RSS

    Array program

    807601
      can someone explain to me how array works? can someone use below example to come out with the code and explain to me?


      Write a program that asks the user to enter any number of integers that are in the range of 0 to 30 inclusive and count how many occurrences of each number are entered. Use a suitable sentinel to signal the end of input. Print out only the numbers (with the number of occurrences) that were entered one or more times.
        • 1. Re: Array program
          807601
          [Arrays Tutorial|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]

          No, I'm not doing your homework for you.
          • 2. Re: Array program
            807601
            can someone explain to me how array works?
            [The Java? Tutorial - Arrays|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
            can someone use below example to come out with the code and explain to me?
            I wouldn't use arrays in the problem you posted.

            ~
            • 3. Re: Array program
              800296
              I can see know why people got irritated when I asked this question. I see that it is similiar to this.
              int x = 5;
              while(x == 5)
              {
              System.out.println("Anyone here explain arrays to me?"); // everday code
              }
              • 4. Re: Array program
                807601
                yawmark wrote:
                I wouldn't use arrays in the problem you posted.
                Would you use an ArrayList?
                • 5. Re: Array program
                  807601
                  Would you use an ArrayList?
                  Upon re-reading the problem, I rescind my comment about not using an array (especially if the assignment requires the use of an array).

                  Typically for histograms, I would use a Map, but this particular histogram is particularly conducive to the use of an array. My bad for not reading more closely to begin with.

                  ~
                  • 6. Re: Array program
                    807601
                    I only wish you guy can guide me through this. I read the tutorial but still not able to catch them. Below are some portion of the program that I wrote. The part I don't understand is when we declare the array, how should I actually link the key in data to the index?

                    import java.util.Scanner;

                    public class SentinelLoop
                    {

                    public static void main (String[]args)
                    {
                    int number,count=0;
                    int [ ] num = new int[count];

                    Scanner read = new Scanner(System.in);
                    number = read.nextInt();


                    // System.out.println("Enter an integer number(-1 to stop>");

                    while ( number>=0 && number<=30)
                    {
                    System.out.println("Enter an integer number(-1 to stop>");
                    number = read.nextInt();
                    count++;
                    }
                    • 7. Re: Array program
                      807601
                      go through java balguruswami book you will get your answer.Workout yourself you will understand better.
                      • 8. Re: Array program
                        807601
                        I only wish you guy can guide me through this.
                        First, you'll need an array of 31 elements to store the counts. As a user enters a number n (0 through 30), increment the value stored at array[n]. Make sense?

                        ~
                        • 9. Re: Array program
                          807601
                          yes. But I'm still confused with the array name assign, the number key in and also how to put it in output. Below is the code i wrote. When I execute, nothing happen. Why?

                          import java.util.Scanner;

                          public class Try
                          {
                               public static void main (String []args)
                               {
                               Scanner read = new Scanner(System.in);

                               int list=0, count=0;
                               int [] num=new int[count];
                               
                               while (list>=0 && list<=30)
                               {

                               System.out.println("Enter the numbers:");
                               list=read.nextInt();

                               }

                               for (int i=0;i<num.length;i++)
                               {
                               System.out.println("Key in is number:"+num);
                               System.out.println("Occurance is :" +count);
                               }

                               
                               }


                          }
                          • 10. Re: Array program
                            807601
                            Only problem with using an array is the instructions say
                            enter any number of integers
                            so you have no idea how big to make the array.


                            DOH! I (like yawmark) didn't read the instructions fully.

                            Edited by: flounder on Apr 15, 2008 9:48 AM
                            • 11. Re: Array program
                              807601
                              yah. Understand that array size might be big....

                              I managed to compile the code, but not able to run coz it is array is out of bound. can someone point out where i don;t wrongly?

                              import java.util.Scanner;

                              public class Try
                              {
                                   public static void main (String []args)
                                   {
                                   Scanner read = new Scanner(System.in);

                                   int list=0;
                                   int count=0;
                                   int [] num=new int[count];
                                   int i=0;

                              while (list>=0 && list<=30)
                                   {

                                   System.out.println("Enter the numbers:");
                                   list=read.nextInt();
                                   num= list;
                                   count++;
                                   i++;
                                   }
                                   
                                   
                              for(i=0;i<num.length;i++)
                                   {
                                   System.out.println("Key in is number:"+num[i]);
                                   System.out.println("Occurance is :" +count);
                                   }

                                   }


                              }
                              • 12. Re: Array program
                                807601
                                int count=0;
                                int [] num=new int[count];
                                You have created an array with a size of zero. Of course you will get an AIOOBE when you try to do anything with it.

                                P.S. Did you bother to read reply #8. It gives you a very big hint on how to do this.