Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week01;
- // Imports
- import java.util.Scanner;
- /**
- * Counter.java, Counts number of words and lines from user input.
- *
- * @author bcollins
- * @version 0.1
- */
- public class Counter{
- private static int lineCount;
- private static int wordCount;
- private static String input;
- /**
- * This is the main method. All processing is done here.
- * @param args not used
- * @return void
- */
- public static void main (String[] args){
- Scanner scan = new Scanner (System.in);
- while (scan.hasNextLine()){
- input = scan.nextLine(); // String of current line
- Scanner inputScan = new Scanner (input);
- while (inputScan.hasNext()){ // While line has a next word
- wordCount++;
- inputScan.next();
- } // end nested while loop
- lineCount++;
- } // end while loop
- System.out.println ("lines: " + lineCount);
- System.out.println ("words: " + wordCount);
- } // end main method
- } // end Counter class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement