brainfrz

Number Reader

Jan 21st, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package numberreader;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class NumberReader {
  6.     final static int MIN = 0;
  7.     final static int MAX = 50;
  8.     final static int NUMBERS = 20;
  9.  
  10.     private static final int[] numberList = new int[NUMBERS];
  11.     private static final int[] occurences = new int[MAX + 1];
  12.  
  13.  
  14.     public static int readInt(int min, int max) {
  15.         Scanner input = new Scanner(System.in);
  16.         int num;
  17.  
  18.         do {
  19.             System.out.print("Enter a number between " + min + " and " + max + ": ");
  20.             num = Integer.parseInt(input.nextLine());
  21.  
  22.             if (num < min || num > max) {
  23.                 System.out.print("Input out of range. ");
  24.             }
  25.         } while (num < min || num > max);
  26.  
  27.         return num;
  28.     }
  29.  
  30.     public static int incrementOccurence(int num) {
  31.         return ++occurences[num];
  32.     }
  33.  
  34.     public static String occurencesTable() {
  35.         String ret = "Inputs  Occurences\n";
  36.         int rowsPrinted = 0;
  37.  
  38.         for (int number = 0; number < occurences.length; number++) {
  39.             if (occurences[number] > 0) {
  40.                 ret += String.format("%-6d  %d\n", number, occurences[number]);
  41.                 rowsPrinted++;
  42.             }
  43.         }
  44.  
  45.         if (rowsPrinted == 0) {
  46.             ret = "No numbers have been entered yet!\n";
  47.         }
  48.  
  49.         return ret;
  50.     }
  51.  
  52.  
  53.     public static void main(String[] args) {
  54.         int currentNumber;
  55.  
  56.         for (int i = 0; i < NUMBERS; i++) {
  57.             currentNumber = readInt(MIN, MAX);
  58.             numberList[i] = currentNumber;
  59.             incrementOccurence(currentNumber);
  60.         }
  61.  
  62.         System.out.println(occurencesTable());
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment