Results 1 to 3 of 3

Thread: Java help!

  1. #1
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Java help!

    Hello all,
    I have been working on a Java program for the better part of 10 hours straight and need a little help finishing the project. The program is desgined to create a list of 10 random numbers, the list is then transveresed by an iterator to count the number of times a random number appears then a second iterator to display the number and how many times it occurs in the list. I think everything will work properly, everything compiles fine, but I cannot figure out where an output statement needs to go to print x + " occurs " + count " times.". Any advise on where this should go?

    import java.util.Random;
    import java.util.Iterator;

    public class countNums
    {

    public static void main(String[] args)
    {

    Random randomGenerator = new Random();
    LitList<Integer> L = new LitList(),
    L2 = new LitList();
    Iterator it = L.getIterator(),
    it2;

    for (int idx = 1; idx <= 10; idx++)
    {
    int ran = randomGenerator.nextInt(50);
    // System.out.println(ran);
    L.add(ran);
    }

    //L.display();

    while (it.hasNext())
    {
    int x = (Integer) it.next();

    if (!L2.contains(x))
    {
    L2.add(x);
    it2 = L.getIterator();

    int count = 0;
    // loop to count how many times it2.next()
    // returns x.
    while (it2.hasNext())
    {
    int y = (Integer) it2.next();
    if (y == x)
    {
    count++;

    }

    }
    //L2.display();

    System.out.println(x + " occurs " + count + " times");
    } // if


    } // while

    } // main

    } // class countNums

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    why not build a map , where the key is the number and the map entry is the count of occurences of the random thing ?

    Java Code:
    Random randomGenerator = new Random();
    
    Map<Integer, Integer> randomNumers = new TreeMap<Integer, Integer>();
    int samples = 10;
    int range = 50;
    
    for (int i = 0; i < samples; i++) {
      Integer randomNum = new Integer(randomGenerator.nextInt(range));
      Integer count = samples.get(randomNum);
      if (count == null) {
        count  = new Integer(0);
      }
      count = new Integer(count.intValue() + 1);
      samples.put(randomNum, count);
    } // for
    
    for (Integer number : samples.keySet()) {
      Integer count = samples.get(number);
      System.out.println(" " + number + " occurs " + count + " times.");
    }

  3. #3
    airboss16 is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default No can do....

    I agree that that would be a much more efficent soultion and be a whole lot easier than what I have not but I cannot use that method. It is an assignment for school and the chapter is over iterators so the instructor is forcing us to use iterators in out projects. Thanks for the adivce though....

Posting Permissions

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