Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package HWPres5;
- import java.util.Scanner;
- public class Arry2test {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- char num;
- int result = 0;
- String[] arrayNum = new String[5];
- String numStrin = null;
- System.out.printf(" Please, Enter %d Numbers : ", arrayNum.length);
- for (int i = 0; i < arrayNum.length; i++) {
- arrayNum[i] = (s.next());
- numStrin = numStrin + arrayNum[i];
- //System.out.println(arrayNum[i]);
- }
- System.out.println(" What DIGIT would you like to check its occurrences?");
- num = s.next().charAt(0);
- //Call local function to count occurrences of digit 3 and print
- System.out.println("The Digit "+num+" was repeated "+countNum(result,numStrin,num)+" Times");
- }
- //Define a public function that returns an Integer value of the occurrence of a digit
- public static int countNum(int count, String numStrin,char num){
- int result=0; // Initialize result to 0
- for (int i=0; i < numStrin.length(); i++){
- if(numStrin.charAt(i) == num )
- {
- result += 1; // Increment Result by 1
- }
- }
- return result; //Return the value of occurrences
- }
- }
- package HWPres5;
- import java.util.Scanner;
- public class StrCount2 {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- char num;
- int result=0;
- String numStrin ;
- //Prompt user for Number
- System.out.println("Enter a number");
- numStrin=(s.next());
- System.out.println(" What DIGIT would you like to check its occurrences?");
- num = s.next().charAt(0);
- //Call local function to count occurrences of digit 3 and print
- System.out.println("The Digit "+num+" was repeated "+countNum(result,numStrin,num)+" Times");
- }
- //Define a public function that returns an Integer value of the occurrence of a digit
- public static int countNum(int count, String numStrin,char num){
- int result=0; // Initialize result to 0
- for (int i=0; i < numStrin.length(); i++){
- if(numStrin.charAt(i) == num )
- {
- result += 1; // Increment Result by 1
- }
- }
- return result; //Return the value of occurrences
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement