RE: [EASY] Programming Challenge 07-27-2016, 05:46 AM
#15
Here's my solution in Java:
Spoiler:
Code:
public static void main(String[] args) {
int lines = 0; //Number of lines in the text file
int chars = 0; //Number of characters in the text file
List<String> text; //Stores all the items from the text file
Scanner textFile = new Scanner(System.in);//Scanner Object
System.out.println("Path to To Text File: ");
String path = textFile.nextLine();//Read User input
File file = new File(path);
if(file.isFile()) { //Check if file exists
try(BufferedReader read = new BufferedReader(new FileReader(path))) {
text = read.lines().collect(Collectors.toList());//Convert Stream to List
chars = text.stream().map((items) -> (items.replaceAll("\\s+", "")).length()).reduce(chars, Integer::sum);//Reduce to Chars and count
//Print Results
System.out.println("Total Lines: " + text.size());
System.out.println("Total Characters: " + chars);
} catch (Exception e) {
System.out.println(e);
}
} else {
System.out.println("File not found!");
}
}


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)