1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * Searches the data file and prints out the stats of the file. */ public static void wordStats() throws Exception { File file = new File( "words.txt" ); Scanner in = new Scanner( new FileInputStream(file)); int words = 1 ; String longestWord = new String(); String shortestWord = new String(); String line = in.nextLine(); line = line.trim(); char ch[]= new char [line.length()]; try { for ( int i= 0 ;i<line.length();i++){ ch[i]= line.charAt(i); if ( ((i> 0 )&&(ch[i]!= ' ' )&&(ch[i- 1 ]== ' ' )) || ((ch[ 0 ]!= ' ' )&&(i== 0 )) ){ words++; } } System.out.println( "There are " +words+ " words." ); System.out.println( "The longest word is " +longestWord+ "." ); System.out.println( "The shortest word is " +shortestWord+ "." ); } catch (Exception e) { System.out.println(e.toString()); } } |

