java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
来源:互联网 发布:linux删除用户组命令 编辑:程序博客网 时间:2024/06/05 13:22
程序小白,希望和大家多交流,共同学习
import java.util.Scanner;import java.io.File;import java.io.PrintWriter;public class DeleteString{ public static void main(String [] args) throws Exception { if (args.length != 2) { System.out.println("Useage: delete string + filename"); System.exit(1); } File file = new File(args[1]); if (!file.exists()) { System.out.println(args[1] + " is not exists"); System.exit(2); } try( Scanner input = new Scanner(file); PrintWriter output = new PrintWriter("newResource.txt"); ){ while (input.hasNext()) { StringBuilder str = new StringBuilder(input.nextLine()); // String 字符串一旦创建就是不可改变的,而且没有delete方法 // 即使使用 replace 方法,替换掉匹配的字符串,但是无法多次替换 // 也就是说在不改变原有字符串的情况下,只能替换一次 int startIndex = str.indexOf(args[0]); while (startIndex != -1) { int endIndex = startIndex + args[0].length() + 1; str.delete(startIndex, endIndex); startIndex = str.indexOf(args[0], endIndex); } output.println(str); } } }}
import java.util.Scanner;import java.io.File;import java.io.PrintWriter;import java.util.ArrayList;public class Reformatting{ public static void main(String [] args) throws Exception{ if (args.length != 1){ System.out.println("Enter a sourcet file"); System.exit(1); } File file = new File(args[0]); if (!file.exists()){ System.out.println("File : " + args[0] + " is not exist"); System.exit(2); } try( PrintWriter output = new PrintWriter("targetFile.txt"); Scanner input = new Scanner(file); ){ // 使用ArrayList<String> 将原有的文件拷贝 // 然后,将里面的 { 单行去除,并在前一行加上{ // 利用了ArrayList 的大小可变的有点 ArrayList<String> recode = new ArrayList<>(); while (input.hasNextLine()){ recode.add(new String(input.nextLine())); } for (int i = 0; i < recode.size() - 1; i++){ if (recode.get(i + 1).contains("{")){ String newLine = recode.get(i) + "{"; recode.set(i, newLine); // set 可以在指定位置设置元素,若已有则进行替换 recode.remove(i + 1); } } for (int i = 0; i < recode.size(); i++){ System.out.println(recode.get(i)); } } }}// 此时的文件就是已经转换过的
12.13 方法一
import java.util.Scanner;import java.io.File;public class Statistics{ public static void main(String [] args) throws Exception { if (args.length != 1) { System.out.println("Useage: file name"); } int countWords = 0; int countLetter = 0; int countLine = 0; Scanner input = new Scanner(new File(args[0])); while (input.hasNextLine()) { String strLine = input.nextLine(); String[] newStrLine = strLine.split(" "); // 使用的是 String 中的 split 先按照空格划分,在连续的空格处 // 会出现空格为单独一行的状况 // 所以要判断每一行是否为一个单词 for (int i = 0; i < newStrLine.length; i++) { if (Character.isLetter(newStrLine[i].charAt(0))) { countWords++; countLetter += newStrLine[i].length(); } } countLine++; } System.out.println("File " + args[0]); System.out.println(countLetter + " character"); System.out.println(countWords + " words"); System.out.println(countLine + " lines"); input.close(); }}
12.13 方法二
import java.util.Scanner;import java.io.File;public class Statistics2{ public static void main(String [] args) throws Exception { if (args.length != 1) { System.out.println("Useage: file name"); } int countWords = 0; int countLetters = 0; int countLines = 0; Scanner input = new Scanner(new File(args[0])); while (input.hasNextLine()) { String strLine = input.nextLine(); // 使用 Charactetr 中的判断每一行的每个元素是否是字符 for (int i = 0; i < strLine.length(); i++) { if (Character.isLetter(strLine.charAt(i))) { countLetters++; } } // 依照每个单词以空格结束来计算单词数 // 因为空格不一定是一个,所以要判断前一个是否是字符 // 最后的一个单词最后可能没有空格就是,而是直接结束 // 所以在最后还要判断每一行的结尾是否是字符 for (int i = 0; i < strLine.length() - 1; i++) { if (Character.isLetter(strLine.charAt(i)) && strLine.charAt(i + 1) == ' ') { countWords++; } } if (Character.isLetter(strLine.charAt(strLine.length() - 1))) { countWords++; } countLines++; } System.out.println("File " + args[0]); System.out.println(countLetters + " character"); System.out.println(countWords + " words"); System.out.println(countLines + " lines"); input.close(); }}
12.14
import java.util.Scanner;import java.io.File;public class TheScoreInFile{ public static void main(String [] args) throws Exception { Scanner input = new Scanner(System.in); System.out.print("Enter the name of a file: "); String fileName = input.nextLine(); File file = new File(fileName); if (!file.exists()) { System.out.println(fileName + " is not exists"); System.exit(1); } double totalScore = 0.0; int count = 0; Scanner in = new Scanner(file); System.out.println("loading..."); while (in.hasNextInt()) { System.out.println("loading..."); totalScore += in.nextDouble(); in.skip(" "); // API 中 Scanner 的一个方法 // 跳过与从指定字符串构造的模式匹配的输入信息。 // 注意在文档的最后一个数字之后也要有空格 count++; } input.close(); in.close(); System.out.println("Total scores is " + totalScore); System.out.println("Average is " + (totalScore / count)); }}
阅读全文
0 0
- java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
- java语言程序设计 第十二章(12.7、12.8、12.9)
- java语言程序设计 第十二章 (12.15、12.16、12.17)
- java语言程序设计 第十二章 (12.18、12.19、12.20、12.21、12.22)
- java语言程序设计 第十二章 (12.23、12.24、12.25、12.26)
- java语言程序设计 第十二章 (12.28、12.30、12.33)
- java 语言程序设计 第十二章(12.1、12.2、12.3、12.4、12.5、12.6)
- java语言程序设计基础篇第十二章编程练习题
- Java 语言程序设计基础篇原书第八版_第十二章_第八题_程序分享
- java语言程序设计 第八章(8.35、8.36)
- java语言程序设计 第七章(7.21、7.35)
- java语言程序设计 第十一章(11.8 11.9)
- java语言程序设计 第十三章 (13.6、13.7)
- java语言程序设计 第十三章 (13.17、13.18)
- chapter1(JAVA语言程序设计)
- Java语言程序设计(序)
- java语言程序设计 第十一章 11.3
- Java语言程序设计 第13章
- 反射
- SSM简单整合(Spring+SpringMVC+Mybatis)
- 面向接口编程 一
- opencv学习之Canny算子
- 获取元素样式
- java语言程序设计 第十二章 (12.11、12.12、12.13、12.14)
- Java集合详解
- tomcat 部署多个项目
- 洛谷 P1122 最大子树和
- 求二叉树的深度
- Github生成SSH key并加入管理
- HDU1452:Happy 2004(数论 & 唯一分解)
- 卡片-问题和答案
- 51 nod 中国剩余定理