【问题标题】:Reading Input From Text File and print the salary and the name of employe从文本文件中读取输入并打印工资和员工姓名
【发布时间】:2014-11-30 12:40:09
【问题描述】:

我需要读取用户输入的薪水,然后打印员工的姓名和姓氏 (姓氏必须为大写字母)使用文本文件中的数字格式对象的薪水超过指定值。 如果输入的薪水小于100,则打印消息''薪水必须大于100',如果大于文件中的薪水,则打印消息“没有员工有这个薪水。”

文本文件名为employee.text 包含员工的姓名和姓氏,城市, 部门、工资和日期

卡尔约翰逊巴黎生产 900 20/07/2000

Wiley Cyrus 伊斯坦布尔采购 800 21/05/2005

哈利波特伦敦会计 780 30/05/2001

Bruce Wayne Gotham 质量 450 05/08/2007

这是我的代码到目前为止我没有得到输出?

Scanner scanFile = new Scanner(new File("Employee.txt"));

NumberFormat fmt = NumberFormat.getCurrencyInstance(Locale.US);

Scanner scan = new Scanner(System.in);

System.out.println("Enter salary: ");
int salary = scan.nextInt();
if(salary < 100)
	System.out.println("The salary must be bigger than 100");
else 
	if(salary > 900)
{
						
	System.out.println("There is no employee has this salary.");
						
}
			
while(scanFile.hasNextInt())
{
	int sal = scanFile.nextInt();
	String code = scanFile.next();
	if(sal >= salary)
	    System.out.println(code + fmt.format(sal));
			
			
}			

【问题讨论】:

    标签: java javascript


    【解决方案1】:

    在你必须检查 hasNextline() 而不是 hasNextInt() 时:

    while (scanFile.hasNextLine()) {
    

    在你的 int sal = scanFile.nextInt();您将在该行中找到 int - 在此示例中,您可以这样做:

    String nextLine = scanFile.nextLine();
    int sal = Integer.parseInt(nextLine.substring(nextLine.lastIndexOf(" ")-3, nextLine.lastIndexOf(" ")));
    

    但您必须进行试验,使其适用于所有情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2018-10-29
      • 2017-01-07
      • 2021-11-20
      相关资源
      最近更新 更多