7-1 jmu-Java-m07 员工信息录入 (15分)

这篇博客介绍了一个Java程序,用于读取用户输入的员工信息,包括id、name、age、position和salary。程序定义了Employee类并实现了异常处理机制。当输入数据个数不匹配、数据类型错误或id重复时,程序会抛出相应的异常信息。博客提供了输入输出示例,并展示了如何处理这些异常情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7-1 jmu-Java-m07 员工信息录入 (15分)
该程序包含:

Employee类:

属性:int id,String name, int age, String position,double salary

方法:getter/setter方法,toString()方法

main方法:

输入一个数num

接下去输入num行的id,name,age,position,salary,数据中间以空格进行区分

对num行的数据进行处理,生成员工列表。使用try/catch对该过程进行异常捕获处理。
如果每行数据输入的个数与所要求的不一致(如,本例中要求员工信息录行需有5个数据,然而有一行只有4个),则抛出异常提示行号以及提示信息”input error”;如果输入的数据类型不匹配,则输出行号并输出相应错误提示信息; 如果id重复,则输出错误行号并提示信息”id repeat”。

最后输出员工列表信息

输入格式:
第一行输入一个整数num

第2~num行输入id name age position salary,数据中间以空格进行分开

输出格式:
输出员工信息列表,如果有异常则先处理异常并输出提示信息

输入样例:
5
1 Andy 20 PM 3000.67
1 Rich 22 CEO 4000
2 Olive 30 CIO 6000.89
3 Jack 34 CFO salary
five fake PM

输出样例:
line:2 java.lang.Exception: id repeat
line:4 java.lang.NumberFormatException: For input string: “salary”
line:5 java.lang.Exception: input error
Employee [id=1, name=Andy, age=20, position=PM, salary=3000.67]
Employee [id=2, name=Olive, age=30, position=CIO, salary=6000.89]

作者
郑如滨
单位
集美大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB


import java.util.*;

public class Main {
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		ArrayList<Employee> employee = new ArrayList<>();
	
		int num = sc.nextInt();
		sc.nextLine();		
		
		for(int i=0;i<num;i++)
		{
			String input=sc.nextLine();
			String []e=input.split("\\s+");
//			System.out.println(e.length);//
//			for(String x:e) {
//				System.out.println(x);
//			}//test
			int j=i+1;
			try {
				if(e.length!=5)
				{
					throw new Exception("input error");
					
				}
				else {
					int id = 0;
					try{
						 id=Integer.parseInt(e[0]);
					}catch(NumberFormatException ex)
					{	
						System.out.println("line:"+j+" java.lang.NumberFormatException: "+ex.getMessage());
					}
					for(Employee x:employee)
					{
						try{
							if(x.getId()==id){
								id=0;
								throw new Exception("id repeat");
							
							}
						
						}catch(Exception ex){
							System.out.println("line:"+j+" java.lang.Exception: "+ex.getMessage());
						}
					}			
					String name=e[1];
					
					int age=0;
					
					try{
						 age=Integer.parseInt(e[2]);
					}catch(NumberFormatException ex)
					{
						System.out.println("line:"+j+" java.lang.NumberFormatException: "+ex.getMessage());
					}
					
					
					String position=e[3];
					
					double salary=0;
					try{
						salary=Double.parseDouble(e[4]);
					}catch(NumberFormatException ex)
					{
						System.out.println("line:"+j+" java.lang.NumberFormatException: "+ex.getMessage());
					}
			
					
					if(salary!=0&&age!=0&&id!=0)
					{
						Employee ee = new Employee(id,name,age,position,salary);
						employee.add(ee);
					}
				}
			}catch(Exception ex)
			{
				System.out.println("line:"+j+" java.lang.Exception: "+ex.getMessage());
			}
	
		}
		for(Employee p: employee)
		{
			System.out.println(p);
		}
		
		
	}
}
class Employee
{
	private int id;
	private String name;
	private int age;
	private String position;
	private double salary;
	
	public Employee(int id,String name, int age, String position,double salary)
	{
		this.id=id;
		this.name=name;
		this.age=age;
		this.position=position;
		this.salary=salary;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String toString()
	{
		return "Employee [id="+getId()+", name="+getName()+", age="+getAge()+", position="+getPosition()+", salary="+getSalary()+"]";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值