File tree Expand file tree Collapse file tree
src/main/java/homework/section16 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package homework .section16 ;
2+
3+ import java .io .File ;
4+ import java .io .FileNotFoundException ;
5+ import java .util .Scanner ;
6+
7+ public class CalSumFromFile {
8+
9+ public static void main (String [] args ) {
10+ double sum = 0 ;
11+ int count = 0 ;
12+ System .out .print ("Please input the file path: " );
13+ Scanner scanner = new Scanner (System .in );
14+ String filePath = scanner .nextLine ();
15+ File file = new File (filePath );
16+ if (!file .exists ()) {
17+ System .out .println ("No such file!" );
18+ System .exit (1 );
19+ }
20+ scanner .close ();
21+
22+ try (
23+ Scanner input = new Scanner (file );
24+ ) {
25+ while (input .hasNext ()) {
26+ sum += input .nextDouble ();
27+ count ++;
28+ }
29+ System .out .println ("There are " + count + " numbers" );
30+ System .out .printf ("The average is: %.2f%n" , (sum / count ));
31+ } catch (FileNotFoundException e ) {
32+ System .out .println ("No such file!" );
33+ System .exit (1 );
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package homework .section16 ;
2+
3+ import java .io .File ;
4+ import java .io .FileNotFoundException ;
5+ import java .util .Scanner ;
6+
7+ public class CheckSortedStrings {
8+
9+ private final static String FILEPATH = "src/main/java/homework/section16/SortedStrings.txt" ;
10+
11+ public static void main (String [] args ) {
12+ File file = new File (FILEPATH );
13+ if (!file .exists ()) {
14+ System .out .println ("No such File!" );
15+ System .exit (1 );
16+ }
17+
18+ String prev = "" ;
19+ try (
20+ Scanner input = new Scanner (file );
21+ ) {
22+ if (input .hasNext ()) {
23+ prev = input .next ();
24+ }
25+ while (input .hasNext ()) {
26+ String cur = input .next ();
27+ if (prev .compareTo (cur ) > 0 ) {
28+ System .out .println ("The strings are not sorted correctly!" );
29+ System .out .println ("The strings " + prev + " and " + cur + " are out of order" );
30+ System .exit (0 );
31+ }
32+ prev = cur ;
33+ }
34+ System .out .println ("The file is sorted!" );
35+ } catch (FileNotFoundException e ) {
36+ System .out .println ("No such File!" );
37+ System .exit (1 );
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package homework .section16 ;
2+
3+ import java .io .File ;
4+ import java .io .FileNotFoundException ;
5+ import java .util .Scanner ;
6+
7+ public class CountLetterAppearance {
8+
9+ public static void main (String [] args ) {
10+ int [] count = new int [26 ];
11+ Scanner sin = new Scanner (System .in );
12+ System .out .print ("Please input the file path: " );
13+ File file = new File (sin .nextLine ());
14+
15+ try (
16+ Scanner input = new Scanner (file );
17+ ) {
18+ while (input .hasNext ()) {
19+ String cur = input .next ().toLowerCase ();
20+ for (char c : cur .toCharArray ()) {
21+ if (c - 'a' > 25 || c - 'a' < 0 ) continue ;
22+ count [c - 'a' ]++;
23+ }
24+ }
25+
26+ System .out .printf ("%6s%15s\n " , "Letter" , "Appearance" );
27+ for (int i = 0 ; i < 26 ; ++i ) {
28+ System .out .printf ("%6c%15d\n " , ('a' + i ), count [i ]);
29+ }
30+
31+ } catch (FileNotFoundException e ) {
32+ System .out .println ("No such file!" );
33+ System .exit (1 );
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ -2122884341 -1876058827 -1869784201 -1808565164 -1780083564 -1691909972 -1675110791 -1624663561 -1600165003 -1592455317 -1541092642 -1520883765 -1494155243 -1388384438 -1353598271 -1324528454 -1192484737 -1090840735 -1040703793 -958514967 -957994911 -925100431 -904333620 -890308802 -830570528 -753053912 -747024071 -728806293 -727246932 -692229979 -588445594 -588022576 -538824401 -532789202 -516059437 -483953739 -381741764 -367838934 -365414193 -335160509 -231362797 -185072281 -98713485 -97465169 23227647 46262400 107652664 115159544 162982647 169162235 172701352 230681334 312865391 313148558 363530575 536352222 559819759 563212881 598484888 606196068 626533490 628982498 657571435 659146120 686809878 687933412 730331334 769517402 862097240 872945511 969552185 980214920 994025617 1001799078 1050225242 1059161715 1105384507 1107121295 1142080214 1261313601 1277868817 1388483110 1463608464 1575306213 1589572329 1646382179 1686582998 1693897186 1749901535 1782691511 1835074942 1861626214 1913114566 1979759376 2036408026 2044123558 2047977748 2075939657 2097355278 2140865520
Original file line number Diff line number Diff line change 1+ package homework .section16 ;
2+
3+ import java .io .File ;
4+ import java .io .FileNotFoundException ;
5+ import java .io .PrintWriter ;
6+ import java .util .Arrays ;
7+ import java .util .Random ;
8+ import java .util .Scanner ;
9+
10+ public class Generate100IntToFile {
11+
12+ private final static String FILEPATH = "src/main/java/homework/section16/Exercise12_15.txt" ;
13+
14+ public static void main (String [] args ) {
15+ Random r = new Random ();
16+ File file = new File (FILEPATH );
17+ try (
18+ PrintWriter writer = new PrintWriter (file );
19+ ) {
20+ for (int i = 0 ; i < 100 ; ++i ) {
21+ writer .print (r .nextInt ());
22+ writer .print (" " );
23+ }
24+ } catch (FileNotFoundException e ) {
25+ System .out .println ("Cannot create the file" );
26+ System .exit (1 );
27+ }
28+
29+ int [] array = new int [100 ];
30+ try (
31+ Scanner input = new Scanner (file );
32+ ) {
33+ int i = 0 ;
34+ while (input .hasNext ()) {
35+ array [i ++] = input .nextInt ();
36+ }
37+ } catch (FileNotFoundException e ) {
38+ System .out .println ("File not found" );
39+ System .exit (2 );
40+ }
41+
42+ Arrays .sort (array );
43+
44+ try (
45+ PrintWriter writer = new PrintWriter (file );
46+ ) {
47+ for (int n : array ) {
48+ writer .print (n );
49+ writer .print (" " );
50+ }
51+ } catch (FileNotFoundException e ) {
52+ System .out .println ("Cannot create the file" );
53+ System .exit (1 );
54+ }
55+ }
56+ }
Original file line number Diff line number Diff line change 1+ package homework .section16 ;
2+
3+ import java .io .File ;
4+ import java .io .FileNotFoundException ;
5+ import java .util .Scanner ;
6+
7+ public class GetFileStatistics {
8+
9+ public static void main (String [] args ) {
10+ if (args == null || args .length == 0 ) {
11+ System .out .println ("Please give file path" );
12+ System .exit (1 );
13+ }
14+ String filePath = args [0 ];
15+ File file = new File (filePath );
16+
17+ int numLine = 0 ;
18+ int numWord = 0 ;
19+ int numCharacter = 0 ;
20+
21+ try (
22+ Scanner input = new Scanner (file );
23+ ) {
24+ while (input .hasNext ()) {
25+ String curLine = input .nextLine ();
26+ numLine ++;
27+ numCharacter += curLine .length ();
28+ curLine = curLine .trim ();
29+ String [] words = curLine .split ("\\ s+" );
30+ numWord += words .length ;
31+ }
32+
33+ System .out .println ("Number of Line: " + numLine );
34+ System .out .println ("Number of Word: " + numWord );
35+ System .out .println ("Number of Character: " + numCharacter );
36+ } catch (FileNotFoundException e ) {
37+ System .out .println ("No such file!" );
38+ System .exit (2 );
39+ }
40+
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ A B C S a b c d e f g h haha steven
Original file line number Diff line number Diff line change 1+ 1.0 2.0 3.9 23.5 63.7 23.3
2+ 1.0 2.0 3.9 23.5 63.7 23.3
3+ 1.0 2.0 3.9 23.5 63.7 23.3
You can’t perform that action at this time.
0 commit comments