Created
October 25, 2014 22:00
-
-
Save andrewjkerr/4e8a9e1bdc23c6ae47c2 to your computer and use it in GitHub Desktop.
Counts the number of occurances for all numbers 1-10 from user input.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Class: CountNum | |
Author: Andrew J Kerr <me@andrewjkerr.com> | |
Summary: Counts the number of occurances for all numbers 1-10 from user input. | |
*/ | |
import java.util.*; | |
public class CountNum | |
{ | |
/// Summary: | |
/// Main method | |
public static void main(String[] args) | |
{ | |
ArrayList<Integer> numbers = getInput(); | |
countNumbers(numbers); | |
} | |
/// Summary: | |
/// Creates an ArrayList of numbers based on the input of a user | |
public static ArrayList<Integer> getInput() | |
{ | |
// Tell user how program operates | |
System.out.println("Enter integers between 1 and 10 on a newline. Enter a 0 to signal the end of your input"); | |
// Create a new ArrayList; will be returned later | |
ArrayList<Integer> numbers = new ArrayList<Integer>(); | |
// Set up the scanner | |
Scanner input = new Scanner(System.in); | |
// Set flag | |
boolean flag = true; | |
// Declare number for use in loop | |
int number = 0; | |
// Loop through user input | |
while(flag) | |
{ | |
// Get user input | |
number = input.nextInt(); | |
// Check if user input == 0 | |
if(number != 0) | |
{ | |
// Check to see whether number is valid or not | |
if(number > 0 && number <= 10) | |
{ | |
// Add number to ArrayList | |
numbers.add(number); | |
} | |
else | |
{ | |
// If not valid, print out error to user | |
System.out.println("Sorry, but you did not enter a number between 1 and 10!"); | |
} | |
} | |
else | |
{ | |
flag = false; | |
} | |
} | |
return numbers; | |
} | |
/// Summary: | |
/// Counts the number of occurances of a number in an ArrayList | |
/// Params: | |
/// ArrayList<int> numbers: an ArrayList containing numbers obtained by user input | |
/// int numToCount: the number to check for number of occurances | |
public static void countNumbers(ArrayList<Integer> numbers) | |
{ | |
// Default numOccurances to 0 | |
int numOccurances = 0; | |
// Iterate through possible numbers | |
for(int i = 1; i <= 10; i++) | |
{ | |
// Call countOccurancesOfNumber | |
numOccurances = countOccurancesOfNumber(numbers, i); | |
// Print it out! :) | |
System.out.println(i + " occurs " + numOccurances + " times."); | |
} | |
} | |
/// Summary: | |
/// Counts the number of occurances of a number in an ArrayList | |
/// Params: | |
/// ArrayList<int> numbers: an ArrayList containing numbers obtained by user input | |
/// int numToCount: the number to check for number of occurances | |
public static int countOccurancesOfNumber(ArrayList<Integer> numbers, int numToCount) | |
{ | |
// Default numOccurances to 0 | |
int numOccurances = 0; | |
// Iterate through ArrayList... linear search, lol. | |
for(Integer i : numbers) | |
{ | |
// If the numbers match up, iterate the count. | |
if(i == numToCount) | |
{ | |
numOccurances++; | |
} | |
} | |
return numOccurances; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment