Results 1 to 4 of 4
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default sample project help

    Design and implement an application that reads an arbitrary number of intergers that are in the range 0-50 inclusive and counts how many occurreces of each are entered. After all input has been processed , print all values(with the number of occurences ) that were entered one or more times..

    Java Code:
    import java.util.Scanner;
    public class SevenPtOne {
    
    
    public static void main(String[] args){
     int x;
    Scanner readMe = new Scanner(System.in);
    System.out.println("Enter couple numbers from 0-50");
    readMe.nextInt();
    for (int i = 0; i <args.length;i++) {
    x = Integer.parseInt(args[i]);
    
    
    // not really sure if i understand the question all the way but heres what i have so far
    // i know i need another for loop but not sure what im checking for..help plz
    		}
    	}
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I'm not really sure you should be looking to use a for loop, but rather a while loop since there is an indefinite number of values coming in. You should also define an exit condition(Maybe if the inputted number is out of the accepted range), to exit the while loop.

    To keep track of the number of occurrences of an inputted number, I suggest using a Map(Map (Java Platform SE 6)). You may want to use a separate List variable(List (Java Platform SE 6)), to hold the unique inputted numbers already added.

    Then after you exit out of the loop, just loop through the numbers already added and get the amount of times they were added from the Map.

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Thanks for the help , when i get home i'll try it out and repost what i have.;)

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since the range of numbers is known beforehand, an array would be sufficient to track the number of occurences:
    Java Code:
    int[] occurences = new int[51]; //this way, you get an array with indexes 0 to 50
    while(someCondition) {
      int num = getInput(); //your method goes here
      occurences[num]++;
    }
    Of course, looking up and learning to use maps would be usefull as well.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Reg: AWT sample application
    By b_srinath_b in forum AWT / Swing
    Replies: 0
    Last Post: 02-15-2010, 01:06 PM
  2. Sample ListViewer
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:41 PM
  3. TextPane Sample
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:46 PM
  4. Sample ANT Script
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:22 PM

Posting Permissions

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