Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. package week01;
  2.  
  3. // Imports
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Counter.java, Counts number of words and lines from user input.
  8.  *
  9.  * @author bcollins
  10.  * @version 0.1
  11.  */
  12. public class Counter{
  13.  
  14.     private static int lineCount;
  15.     private static int wordCount;
  16.     private static String input;
  17.  
  18.     /**
  19.      * This is the main method. All processing is done here.
  20.      * @param args not used
  21.      * @return void
  22.      */
  23.     public static void main (String[] args){
  24.  
  25.         Scanner scan = new Scanner (System.in);
  26.  
  27.     while (scan.hasNextLine()){
  28.  
  29.         input = scan.nextLine(); // String of current line
  30.         Scanner inputScan = new Scanner (input);
  31.        
  32.         while (inputScan.hasNext()){ // While line has a next word
  33.         wordCount++;
  34.         inputScan.next();
  35.         } // end nested while loop
  36.  
  37.         lineCount++;
  38.        
  39.     } // end while loop
  40.    
  41.    
  42.         System.out.println ("lines: " + lineCount);
  43.         System.out.println ("words: " + wordCount);
  44.  
  45.     } // end main method   
  46.  
  47. } // end Counter class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement