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));    }}
原创粉丝点击