Results 1 to 8 of 8

Thread: Question on Arrays

  1. #1

    Thread Starter
    Lively Member MHENK's Avatar
    Join Date
    Sep 2001
    Location
    Wisconsin
    Posts
    99

    Question on Arrays

    Hi guys, been a while...

    anyways, i've decided to teach myself java, and in the direction book that I have, one of the suggested projects that I do is the following, and I was wondering how you guys would go about doing this:

    Design and implement an application that reads an arbitrary number of integers that are in the range of 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 occurances] that were entered one or more times.

    Any Ideas? Thanks a lot, in advance.

    -Michael Henk
    [email protected]
    "Have you ever woken up, looked at yourself in the mirror, and said 'screw the diet, how fat can i possibly get'?"

  2. #2

    Thread Starter
    Lively Member MHENK's Avatar
    Join Date
    Sep 2001
    Location
    Wisconsin
    Posts
    99
    Okay, i know how to create the array and get the users input for the number of entries and the values for the entries itself. I just need to know how to count the number of times each number appears in the array.
    "Have you ever woken up, looked at yourself in the mirror, and said 'screw the diet, how fat can i possibly get'?"

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ive been working on it. The code has a couple of quirks but it might give you a general direction to head in.
    Code:
     public class X{
      public static void main(String[] args){
    
    /*
       for(int i = 0; i < args.length; ++i){  
         if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 49)
           System.out.println("Values must be in the range of 0 to 49");    
           System.exit(0); 
       }
    */ 
       int[] holder = new int[50];
       
       for(int i = 0; i < args.length; ++i){
         int value = Integer.parseInt(args[i]);
         holder[value]++;   
       }
    
       for(int i = 0; i < args.length; ++i){
        if(i != 0){
         if(Integer.parseInt(args[i]) == Integer.parseInt(args[i - 1])){
           continue;
         }else{
          printOcc(args, holder, i);
         }
        }else{
           printOcc(args, holder, i);
         }
        }
       }
      public static void printOcc(String[] args, int[] holder, int index){
        int num = Integer.parseInt(args[index]); 
         System.out.println(args[index] + " appears " + holder[num] + " times ");
      
      }
     }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The following block works to limit the range of values the user enters but for some reason the array is somehow wiped clean afer the block executes thus if the user enters any values outside the indicated range an ArrayIndexOutOfBounds exception is thrown.
    Code:
    for(int i = 0; i < args.length; ++i){  
         if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 49)
           System.out.println("Values must be in the range of 0 to 49");    
           System.exit(0); 
       }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It says 50 inclusive, so increase array size and upper bound of the check by one.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    CornedBee. Any ideas to the problem i posted?
    Code:
    for(int i = 0; i < args.length; ++i){  
         if(Integer.parseInt(args[i]) < 0 || Integer.parseInt(args[i]) > 50)
           System.out.println("Values must be in the range of 0 to 49");    
           System.exit(0); 
       }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I have no idea what you're talking about. To me it seems as if this program should simply exit if there is at least one argument passed. You forgot braces around the if block and the System.exit should therefore be called unconditionally.

    Anyway...
    Code:
    import java.io.*;
    
    class CountNumbers {
      private static Reader in =
        new BufferedReader(new InputStreamReader(System.in));
    
      public static void main(String[] args) {
        int count = readNum();
        int[] nums = new int[51];
        while(count-- > 0) {
          int t;
          do {
            t = readNum();
          } while(t < 0 || t > 50);
          ++nums[t];
        }
        // Now all left to do is write out the array.
      }
    
      public static int readNum() {
        StringBuffer buf = new StringBuffer();
        char c;
        while(Character.isWhitespace(c = in.read()))
          ; 
        while(!Character.isWhitespace(c)) {
          buf.append(c);
          c = in.read();
        }
        return Integer.parseInt(buf.toString());
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yeah sorry forgot to add in the brackets. I was rushing to get ready for a party so i slapped the code together in a rush.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width