Advertisement
rushdie

Function Home Work

Nov 24th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. package HWPres5;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Arry2test {
  6.  
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9.  
  10. char num;
  11. int result = 0;
  12. String[] arrayNum = new String[5];
  13. String numStrin = null;
  14.  
  15. System.out.printf(" Please, Enter %d Numbers : ", arrayNum.length);
  16. for (int i = 0; i < arrayNum.length; i++) {
  17. arrayNum[i] = (s.next());
  18. numStrin = numStrin + arrayNum[i];
  19. //System.out.println(arrayNum[i]);
  20.  
  21. }
  22.  
  23. System.out.println(" What DIGIT would you like to check its occurrences?");
  24. num = s.next().charAt(0);
  25.  
  26. //Call local function to count occurrences of digit 3 and print
  27. System.out.println("The Digit "+num+" was repeated "+countNum(result,numStrin,num)+" Times");
  28.  
  29. }
  30.  
  31.  
  32. //Define a public function that returns an Integer value of the occurrence of a digit
  33. public static int countNum(int count, String numStrin,char num){
  34. int result=0; // Initialize result to 0
  35. for (int i=0; i < numStrin.length(); i++){
  36. if(numStrin.charAt(i) == num )
  37. {
  38. result += 1; // Increment Result by 1
  39. }
  40. }
  41. return result; //Return the value of occurrences
  42. }
  43. }
  44. package HWPres5;
  45.  
  46. import java.util.Scanner;
  47.  
  48.  
  49. public class StrCount2 {
  50.  
  51. public static void main(String[] args) {
  52. Scanner s = new Scanner(System.in);
  53.  
  54. char num;
  55. int result=0;
  56. String numStrin ;
  57.  
  58. //Prompt user for Number
  59. System.out.println("Enter a number");
  60. numStrin=(s.next());
  61. System.out.println(" What DIGIT would you like to check its occurrences?");
  62. num = s.next().charAt(0);
  63. //Call local function to count occurrences of digit 3 and print
  64. System.out.println("The Digit "+num+" was repeated "+countNum(result,numStrin,num)+" Times");
  65.  
  66. }
  67.  
  68.  
  69. //Define a public function that returns an Integer value of the occurrence of a digit
  70. public static int countNum(int count, String numStrin,char num){
  71. int result=0; // Initialize result to 0
  72. for (int i=0; i < numStrin.length(); i++){
  73. if(numStrin.charAt(i) == num )
  74. {
  75. result += 1; // Increment Result by 1
  76. }
  77. }
  78. return result; //Return the value of occurrences
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement