Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef96bd1

Browse files
author
jsquared21
committed
Add Ex 20.11
1 parent872960b commitef96bd1

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
2.48 KB
Binary file not shown.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*********************************************************************************
2+
* (Match grouping symbols) A Java program contains various pairs of grouping *
3+
* symbols, such as: *
4+
* *
5+
* ■ Parentheses: ( and ) *
6+
* ■ Braces: { and } *
7+
* ■ Brackets: [ and ] *
8+
* *
9+
* Note that the grouping symbols cannot overlap. For example, (a{b)} is illegal. *
10+
* Write a program to check whether a Java source-code file has correct pairs of *
11+
* grouping symbols. Pass the source-code file name as a command-line argument. *
12+
*********************************************************************************/
13+
importjava.io.*;
14+
importjava.util.*;
15+
16+
publicclassExercise_20_11 {
17+
publicstaticvoidmain(String[]args)throwsIOException {
18+
// Check command-line argument
19+
if (args.length !=1) {
20+
System.out.println("Usage: Java Exercise_20_11 Source-codeFileName");
21+
System.exit(0);
22+
}
23+
24+
// Check if file exists
25+
Filefile =newFile(args[0]);
26+
if (!file.exists()) {
27+
System.out.println("The file " +args[0] +" does not exist!");
28+
System.exit(1);
29+
}
30+
31+
// Create a stack
32+
Stack<Character>symbols =newStack<>();
33+
34+
try (// Create an input stream for file
35+
Scannerinput =newScanner(file);
36+
) {
37+
// Continuously read chars from input
38+
while (input.hasNext()) {
39+
Stringline =input.nextLine();
40+
for (inti =0;i <line.length();i++) {
41+
charch =line.charAt(i);
42+
// Push symbols (, {, and [ on to the stack
43+
if (ch =='(' ||ch =='{' ||ch =='[') {
44+
symbols.push(ch);
45+
}// Process stack
46+
elseif (ch ==')' ||ch =='}' ||ch ==']') {
47+
processSymbols(symbols,ch);
48+
}
49+
}
50+
}
51+
}
52+
53+
System.out.println("The Java source-code " +
54+
(symbols.isEmpty() ?"has" :"does not have") +" correct pairs.");
55+
}
56+
57+
/** Method Matchs grouping symbols */
58+
privatestaticvoidprocessSymbols(
59+
Stack<Character>stack,Characterch) {
60+
// Remove matching symbols from stack
61+
if ((stack.peek() =='(' &&ch ==')') ||
62+
(stack.peek() =='[' &&ch ==']') ||
63+
(stack.peek() =='{' &&ch =='}')) {
64+
stack.pop();
65+
}
66+
elseif ((stack.peek() !='(' &&ch ==')') ||
67+
(stack.peek() !='[' &&ch ==']') ||
68+
(stack.peek() !='{' &&ch =='}')) {
69+
System.out.println("The Java source-code does not have"
70+
+" correct pairs.");
71+
System.exit(1);
72+
}
73+
}
74+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
publicclassWelcome {
2+
publicstaticvoidmain(String[]args) {
3+
// Display message Welcome to Java! on the console
4+
System.out.println("Welcome to Java!");
5+
}
6+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp