Tracking Occurences in an Array

[b][red]This message was edited by apw420 at 2003-7-1 18:48:40[/red][/b][hr]

Trying to read a number, then after reading the number, printing out the numbers entered and what the occurence of the numbers were.

I'm stuck as I only have what I think is the correct input, but not sure how to track the numbers and put them in an array.

Any help is appreciated.
Here's what I have so far:
[italic]
[blue]
import java.io.*;
public class Occur {

public static void main (String [] args) throws IOException{
String input;
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

int value=0;

int[] array = new int[100];

for (int i=0; i 50 || value < -1)
System.out.println("Invalid Entry");

array[i] = value;
}

System.out.println("This is where I will print out each number entered followed by its occurance" + array);

}
}
[/blue]
[/italic]






Comments

  • Obviously you need an array to keep a statistic over the occurence of values
    [CODE]
    int[] stat = new int[51]; // Values from 0 to 50 => 51 values

    // For every accepted value you increase the corresponding
    // index in stat

    stat[value]++;

    // Print value and occurence
    for(int i=0; i<=50; i++)
    System.out.println(i+": "+stat[i]);
    [/CODE]
  • You may use anther Array of ints of size = 100 to track the number of occurances as suggested, or you can use a Hashtable.
    Hashtable does not accept primatives so you must use one of the Number wrapper classes, ie Integer.

    Hashtable occurances = new Hashtable();

    void putInt(int i)
    {
    Integer I = new Integer(i);
    Integer Count =
    (occurances.contains (I)) ? occurances.get(I) : new Integer(0);
    int count = Count.intValue ()+1;
    Count = new Integer(count);
    occurances.put (I,Count);
    }

    You can get the Keys (the numbers entered) from as an Enumeration by occurances.keys()
    of you can get the keys as a Set from occurances.keySet ().
    The Set can be sorted easily.

    Good Luck
    ~ss~




    : [b][red]This message was edited by apw420 at 2003-7-1 18:48:40[/red][/b][hr]
    :
    : Trying to read a number, then after reading the number, printing out the numbers entered and what the occurence of the numbers were.
    :
    : I'm stuck as I only have what I think is the correct input, but not sure how to track the numbers and put them in an array.
    :
    : Any help is appreciated.
    : Here's what I have so far:
    : [italic]
    : [blue]
    : import java.io.*;
    : public class Occur {
    :
    : public static void main (String [] args) throws IOException{
    : String input;
    : BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
    :
    : int value=0;
    :
    : int[] array = new int[100];
    :
    : for (int i=0; i 50 || value < -1)
    : System.out.println("Invalid Entry");
    :
    : array[i] = value;
    : }
    :
    : System.out.println("This is where I will print out each number entered followed by its occurance" + array);
    :
    : }
    : }
    : [/blue]
    : [/italic]
    :
    :
    :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories