《Java语言程序设计(基础???0?梁勇 ?第二十一章练习题答案 - 百度文库 ر

return line.substring(0, index); }

/* Strip string literals */

private static String stripLineStringLiterals(String line) { int start = line.indexOf(\);

int end = line.indexOf(\, start + 1);

while (start > 0 && end > 0) {

line = line.substring(0, start) + line.substring(end + 1); start = line.indexOf(\); end = line.indexOf(\); }

return line; }

/* Strip paragraph comments */

private static String stripParagraghComments(String text) { int start = text.indexOf(\); int end = text.indexOf(\);

while (start > 0 && end > 0) {

text = text.substring(0, start) + text.substring(end + 2); start = text.indexOf(\); end = text.indexOf(\); }

return text; } }

/** This is an incorrect version. It does not count the case such as (this. Here the this keyword

* is not counted. It does not exclude keywords in the comments *

import java.util.*; import java.io.*;

public class Exercise20_03 {

public static void main(String[] args) { // Check usage

if (args.length != 1) {

System.out.println(\

System.exit(0); }

// Array of all Java keywords + true + null

String[] keywordString = {\ \ \ \ \ \ \ \ \

Set keywordSet =

new HashSet(Arrays.asList(keywordString)); int count = 0; try {

Scanner input = new Scanner(new File(args[0]));

while (input.hasNext()) { String token = input.next(); if (keywordSet.contains(token)) count++; }

System.out.println(\ + count); }

catch (Exception ex) { ex.printStackTrace(); } } } */

21.4

import java.util.Scanner; import java.util.HashSet; import java.util.Arrays;

public class Exercise21_04 {

public static void main(String[] args) throws Exception {

HashSet set1 = new HashSet(Arrays.asList( new Character[]{'A', 'E', 'I', 'O', 'U'}));

System.out.print(\); Scanner input = new Scanner(System.in); String filename = input.nextLine();

input = new Scanner(new java.io.File(filename));

int countVowels = 0; int countConsonants = 0;

while (input.hasNext()) {

String s = input.nextLine().toUpperCase(); for (int i = 0; i < s.length(); i++) { if (set1.contains(s.charAt(i))) countVowels++;

else if (Character.isLetter(s.charAt(i))) countConsonants++; } }

System.out.println(\ + countVowels + \consonanats is \ + countConsonants); } }

21.5

/**

* Usage: Copy this class to the folder, run it with java JavaToHTMLftim * to generate

* HTM file for all the .java file in this folder. The generated .htm files are

* stored in the same folder */

import java.util.*; import java.io.*;

public class Exercise21_05 {

// Array of all Java keywords + true + false + null static String[] keywordString = { \, \, \,

\, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \};

static Set keywordSet =

new HashSet(Arrays.asList(keywordString));

/** Main method */

public static void main(String[] args) throws Exception { // Check usage

if (args.length != 2) { System.out.println(

\); System.exit(1); }

Scanner input = new Scanner(new File(args[0]));

PrintWriter output = new PrintWriter(new File(args[1]));

JavaToHTML(input, output); }

static boolean stringToken = false; static String inputFileName;

public static void JavaToHTML(Scanner input, PrintWriter output) { try {

output.format(\, \); output.format(\, \); output.format(\,

\ + inputFileName + \);

output.format(\,

\charset=windows-1252\\\);

output.format(\, \); output.format(\,