2017-04-21 299 views
0

我幾乎通過這項任務,但我不斷收到錯誤 「unreported exception java.io.IOException; must be caught or declared to be thrown」我是不是寫我的代碼,或者我錯過了一大塊代碼?謝謝你的時間!錯誤:未報告的異常java.io.IOException;必須被捕獲或被宣佈爲被拋出

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(); 
    } 
} 
+0

請學會如何接受的答案,而不是僅僅把它並去...我已經在這裏回答你的問題http://stackoverflow.com/questions/43527657/file-writer-do-while-errors/43529361#43529361 – Yahya

+0

@約翰問題你提到似乎被刪除。 – ventiseis

+0

@ventiseis他發佈了一個代碼充滿錯誤的問題,導致了很多錯誤,我爲它完全重構了它,並在eclipse上運行它並對它進行了測試以確保它,然後我回答了他的問題......然後他將它不接受這個問題 - >現在他提出了同樣的問題,當我接觸他時,他將其刪除了! – Yahya

回答

1

你必須使用拋出IOException這樣的:

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

或者你可以用try{}catch(...){}環繞你的說法:

try { 
    //Your code ... 
} catch (IOException ex) { 
    //exception 
} 
+0

嘿,貼了同樣的東西:p我會刪除我的,因爲你的文字更好 – Imus

+0

哈哈,對不起@Imus謝謝你的編輯:) –

+1

謝謝..我修正了那個錯誤。我運行程序,得到這個錯誤: – user7671

相關問題