Advertisement
brilliant_moves

RunMe.java

Nov 4th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.45 KB | None | 0 0
  1. /*
  2.  Java program with the name RunMe that will take input from the keyboard and will count the number
  3.  of elements that are numbers(int) and the number of elements that are not numbers. The count will stop
  4.  when we give as an input zero 0. Then the programm will display the results and will terminate.
  5.  We also have to develop a class named Counter. We also have to use the class named Scanner.
  6. */
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class RunMe {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner in = new Scanner(System.in);
  14.         Counter counter = new Counter();
  15.         String strInput;
  16.         int n;
  17.         do {
  18.             System.out.print("Enter element (numeric/non-numeric) (zero to quit): ");
  19.             strInput = in.nextLine();
  20.             try {
  21.                 n = Integer.parseInt(strInput);
  22.                 if (n==0) break;
  23.                 counter.incrementNums();
  24.             } catch (NumberFormatException x) {
  25.                 counter.incrementNonNums();
  26.             }
  27.         } while (true);
  28.         System.out.println("Number of numeric elements = "+counter.getNums());
  29.         System.out.println("Number of non-numeric elements = "+counter.getNonNums());
  30.     } // main()
  31.  
  32. } // class RunMe
  33.  
  34. class Counter {
  35.  
  36.     private int numCount;
  37.     private int nonNumCount;
  38.  
  39.     public Counter() {
  40.         numCount = 0;
  41.         nonNumCount = 0;
  42.     }
  43.  
  44.     public void incrementNums() {
  45.         numCount++;
  46.     }
  47.  
  48.     public void incrementNonNums() {
  49.         nonNumCount++;
  50.     }
  51.  
  52.     public int getNums() {
  53.         return numCount;
  54.     }
  55.  
  56.     public int getNonNums() {
  57.         return nonNumCount;
  58.     }
  59. } // class Counter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement