错误:未报告的异常java.io.IOException;必须被抓住或宣布被抛出

时间:2017-04-21 18:43:31

标签: java exception while-loop filewriter

我几乎完成了这项任务但我不断收到错误 “unreported exception java.io.IOException; must be caught or declared to be thrown”我是不按顺序编写代码还是错过了一大堆代码?谢谢你的时间!

import java.io.*;
import java.util.*;
import java.text.*;

public class database1 {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) throws FileNotFoundException {
        String empnumber;
        String firstname;
        String lastname;
        String city;
        String state;
        String zipcode;
        String jobtitle;
        int salary;
        int max = 200000;
        int counter = 0;

        FileWriter ryyt = new FileWriter("c:\\EmployeeData.txt");

        BufferedWriter out = new BufferedWriter(ryyt);

        while (counter < 9) {
            System.out.print("Enter the employee number ...... ");
            empnumber = console.next();

            System.out.print("Enter employee's first name .... ");
            firstname = console.next();

            //firstname = Character.toUpperCase(firstname.charAt(0)) + 
            firstname.substring(1);

            System.out.print("Enter employee's last name ..... ");
            lastname = console.next();
            lastname = Character.toUpperCase(lastname.charAt(0))
                    + lastname.substring(1);

            System.out.print("Enter employee's city .......... ");
            city = console.next();
            city = Character.toUpperCase(city.charAt(0)) + city.substring(1);

            System.out.print("Enter employee's state ......... ");
            state = console.next();
            String upperstate = state.toUpperCase();

            System.out.print("Enter employee's zip code ...... ");
            zipcode = console.next();

            System.out.print("Enter employee's job title ..... ");
            jobtitle = console.next();
            jobtitle = Character.toUpperCase(jobtitle.charAt(0))
                    + jobtitle.substring(1);

            System.out.print("Enter employee's salary ........ ");
            salary = console.nextInt();

            while (salary > max) {
                if (salary > max) {
                    System.out.println(
                            "Salary is over the maxium allowed, re-enter please ...");
                    System.out.print("Enter employee's salary ........ ");
                    salary = console.nextInt();
                } else {
                    System.out.println("Thank you please enter the next employee!");
                }
            }

            System.out.println();

            out.write(empnumber + ",");
            out.write(firstname + ",");
            out.write(lastname + ",");
            out.write(city + ",");
            out.write(upperstate + ",");
            out.write(zipcode + ",");
            out.write(jobtitle + ",");
            out.write(salary + ",");
            out.newLine();

            counter = counter + 1;
        }

        out.close();
    }
}

1 个答案:

答案 0 :(得分:1)

你必须像这样使用投掷IOException

public static void main(String[] args) throws FileNotFoundException, IOException 

或者您可以使用try{}catch(...){}

包围您的陈述
try {
    //Your code ...
} catch (IOException ex) {
    //exception 
}