Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java array

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java array

    Hi, I wrote this code, but the last part I did it by trial and error and by seeing something similar. I don't understand how to print the number of occurrences. I did it, but I don't understand why I had to put " if (array[i] > 0) "


    /*PP 8.1 Design and implement an application that reads an arbitrary
          number of integers that are in the range 0 to 50 inclusive and
         counts how many occurrences of each are entered. After all input
        has been processed, print all of the values (with the number of
       occurrences) that were entered one or more times.
    */
        import java.util.*;
        public class arbArray
        {
            public static void main(String[]args)
            {
               final int FINAL=51;
               int[] array = new int[FINAL];
               Scanner scan = new Scanner(System.in);
     
              for (int i = 0; i < array.length; i++)
               { 
               System.out.println("Enter a number for location "+i+":");
               int n = scan.nextInt(); 
               while (n < 0 || n > 50) 
                { 
                 System.out.println("Please enter a number between 0 and 50 for location "+i+" :"); 
                   n = scan.nextInt(); 
                 } 
                  array[n]++; 
                 }
                for (int i = 0; i < array.length; i++)
                { 
                 if (array[i] > 0) 
                { 
     
                  System.out.println(i + ": " + array[i]); // Print the number of occurrences for the numbers entered.
                 }
              }
     
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java array

    Please read the Announcement topic at the top of the sub-forum to learn how to post your code correctly and other useful tips for newcomers.

    This is a frequency counter. The last sentence of the assignment spec says, "print all of the values (with the number of occurrences) that were entered one or more times." That means, don't print frequencies of zero. That's why the statement you asked about is required.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java array

    Thanks, I hadn't seen the Announcement topic. I fixed now.

    --- Update ---

    Now I see it. I got it, thank you for your help. You said the magic words, "That means, don't print frequencies of zero." Sometimes is difficult to see it until someone gives you a hint.

Similar Threads

  1. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  2. Replies: 4
    Last Post: May 28th, 2013, 11:24 PM
  3. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  4. java array
    By fazmy3fvz in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2012, 01:25 PM
  5. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM