异常处理与文本IO 习题12_24&12_25
来源:互联网 发布:js base64转换为图片 编辑:程序博客网 时间:2024/06/10 15:15
12_24:创建大的数据集:创建一个具有1000行的数据文件。文件中的每行包含了一个教职员工的姓、名、级别以及薪水,第i行的教职员工的姓和名为FristNamei 和LastNamei。级别随机产生为assistant、associate、full。薪水为随机产生的数字,并且小数点后保留两位数字。对于助理教授而言,薪水应该在50000到80000的范围内,对于副教授为60000到110000。对于正教授为75000到130000。保存文件为Salary.txt.
package Pe12_24;import java.io.File;import java.io.PrintWriter;public class Exception12_24 { public static void main(String[] args) throws Exception { String strGardeName = null; StringBuilder sb1 = new StringBuilder(); File tagartFile = new File(args[0]); try (PrintWriter pw = new PrintWriter(tagartFile);) { for (int i = 1; i <= 1000; i++) { sb1.append("FristName" + i + " LastName" + i); strGardeName = getGradeName((int) (Math.random() * 2 + 1)); sb1.append(" "+strGardeName); sb1.append(" "+getSalary(strGardeName)); pw.println(sb1.toString()); sb1.delete(0, sb1.length()); } } } //隨機產生教職員工級別 public static String getGradeName(int i) { String strName = null; if (i > 3 || i < 1) { System.out.println("Uage:getGarde Error."); System.exit(1); } switch (i) { case 1: return strName = "assistant"; case 2: return strName = "associate"; case 3: return strName = "full"; } return strName; }//隨機產生教職員工薪水,返回整數的字符串形式。 public static String getSalary(String gardeName) { String strSalary = null; double salary = 0; if (!(gardeName.equalsIgnoreCase("assistant") || gardeName.equalsIgnoreCase("associate") || gardeName.equalsIgnoreCase("full"))) { System.out.println("Uage: getSalary Error"); System.exit(2); } if (gardeName.equalsIgnoreCase("assistant")) { salary = Math.random() * 30000 + 50000; } else if (gardeName.equalsIgnoreCase("associate")) { salary = Math.random() * 50000 + 60000; } else if (gardeName.equalsIgnoreCase("full")) { salary = Math.random() * 55000 + 75000; } strSalary = String.valueOf(salary); System.out.println(strSalary); int index = strSalary.indexOf('.'); strSalary = strSalary.substring(0, index + 3); return strSalary; }}
**总结:
1、在写入文本的过程中,是使用了StringBuilder来处理字符串,主要就是因为StringBuilder在处理字符串时比较方便。
2、在随机产生教职员工级别的判断上,是采用了Math.random()的方法来随机产生‘1’、‘2’、‘3’来判断返回教职员工的级别。
3、在根据教职员工的级别来随机产生工资薪水的方法上是采用了String.equalsIgnoreCase()方法来判定教职员工的类别,从而使用Math.random()来随机产生规定范围内的工资薪水。**
12_25:一个大学将其教职员工的薪水发布在http://cs.strong.edu/liang/Salary.txt中。文件中的每行包含一个教职员工的姓、名、级别以及薪水。编写一个程序,分别显示助理教授、副教授、正教授,所有教职员工各个类别的总薪水,以及上诉类别的平均薪水。
package Pe12_25;import java.net.URL;import java.util.Scanner;public class Exception12_25 { public static void main(String[] args) throws Exception { double salary_assistant = 0; int count_assistant = 0; double salary_associate = 0; int count_associate = 0; double salary_full = 0; int count_full = 0; double salary_quantity = 0; URL StringURL = new URL("http://cs.armstrong.edu/liang/data/Salary.txt"); String string_line = null; try (Scanner input = new Scanner(StringURL.openStream());) { while (input.hasNext()) { string_line = input.nextLine(); //分隔字符串 String[] strings = string_line.split(" "); //將字符串數組最後一項表示工資薪水的轉化成整數。 salary_quantity = Double.valueOf(strings[strings.length - 1]); //對各個級別的工資和人數進行統計。 if (strings[strings.length - 2].equalsIgnoreCase("assistant")) { salary_assistant += salary_quantity; count_assistant++; } else if (strings[strings.length - 2].equalsIgnoreCase("associate")) { salary_associate += salary_quantity; count_associate++; } else if (strings[strings.length - 2].equalsIgnoreCase("full")) { salary_full += salary_quantity; count_full++; }string_line = null; } System.out.printf("Assistant,count is %d, sum_salary is %.2f,average_salary:%.2f",count_assistant,salary_assistant,salary_assistant/count_assistant); System.out.println(); System.out.printf("Accocite,count is %d, sum_salary is %.2f,average_salary:%.2f",count_associate,salary_associate,salary_associate/count_associate); System.out.println(); System.out.printf("Assistant,count is %d, sum_salary is %.2f,average_salary:%.2f",count_full,salary_full,salary_full/count_full); System.out.println(); } }}
*总结:
1、在读取一行字符串时,分别取出字符串中单独的字符串,使用String.split()以空格作为分隔标志,然后再对单独的字符串进行处理。*
- 异常处理与文本IO 习题12_24&12_25
- Java_SE08-文本数据IO操作,异常处理
- 文本数据IO操作 、 异常处理
- Python基础-IO与文本处理
- Java习题4—IO流与异常
- SE02 Unit03 文本数据IO操作 、 异常处理
- Java异常处理习题
- 【18下】IO流1_FileWriter,IO异常处理,文本读取和拷贝
- IO操作 IO异常处理
- python3--异常处理及习题
- IO 异常处理模式
- JAVA IO异常处理
- IO之异常处理
- IO异常 的处理
- IO异常的处理
- IO异常的处理
- 黑马程序员-day019-字符流与IO异常处理
- IO流部分;对文本写入读出的操作,并对异常作出处理
- 在js(jquery)中获得文本框焦点和失去焦点的方法
- java语言基础(78)——异常处理中finally关键字深入理解
- 多线程
- github 项目推荐之个人版
- 设计模式->行为型模式->备忘录模式
- 异常处理与文本IO 习题12_24&12_25
- ArcGIS水文分析实战教程(4)地形预处理
- git的命令大全
- Android调用系统相机
- Spark Streaming编程指南(二)
- jquery实现点击input选中文字代码
- android webview里设置cookie
- 嵌套循环的使用
- 使用SpringCloud实战微服务