public class Employee {
public static void main(String[] args) {
int j=3;
staples[] stemp = new staples[j];
String file_name = "d:/personal/11636470/NetBeansProjects/Employee/src/employee/Xanadu.txt";
throws IOException
{
Scanner s = null;
try {
s = new Scanner(
new BufferedReader(
new FileReader("file_name")));
while (s.hasNext())
{
System.out.println(s.next());
}
} finally
{
if (s != null)
{
s.close();
}
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (j=0;j<3;j++)
{
stemp[j] = new staples();
System.out.print("Enter your name : ");
stemp[j].setName(reader.readLine());
System.out.println("Enter your age : ");
stemp[j].setAge(Integer.parseInt(reader.readLine()));
}
for (j=0;j<3;j++)
{
System.out.println("Employee number:" + j +" name:"+stemp[j].getName()+" Age:"+stemp[j].getAge());
}
reader.close(); // VERY IMPORTANT TO CLOSE
System.out.println("Program ended");
}
catch(java.io.IOException ex)
{
System.out.println("Error is " + ex.getMessage());
}
}
} }Java異常處理查詢
這個問題似乎很簡單,我得到在「拋出IOException異常」路線錯誤,是有什麼錯的try和catch方法,我實現?
該代碼有兩個部分,一個是讀取文件xanadu.txt,另一個是複製獲取員工數據。兩者都嘗試並捕獲實現。
有點重新格式化可能會有所幫助,我感到困惑的是第10行 – DaveRlz 2012-02-04 18:22:00
是的,代碼縮進遍佈整個地方。 – 2012-02-04 18:22:56
讓我重新格式化它 – 2012-02-04 18:23:33