Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save JorgeOlvera/11205512 to your computer and use it in GitHub Desktop.

Select an option

Save JorgeOlvera/11205512 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.*;
public class countingLines {
public static void main(String [] args) {
try {
//Scans file
Scanner input = new Scanner(System.in);
Scanner file = new Scanner(new File(text2.txt));
//Divides into tokens for second part of output
StringTokenizer muchToken = new StringTokenizer(text2.txt)
int lineCounter = 1;
int wordCounter = 1;
while (file.nextLine() != null){
lineCounter = lineCounter + 1;
}
while (muchToken.hasMoreTokens){
wordCounter = wordCounter + 1;
}
}
catch (NoSuchElementException e) {
break;
}
//Prints counter values (minus 1 to make up for the extra "1" when initialized)
System.out.println("Numer of lines is equal to" + lineCounter-1);
System.out.println("Number of words and/or numbers is equal to" + wordCounter-1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment