我几乎就在那里,但是我在删除异常捕获时遇到错误。第二行到最后一行代码。还有什么方法可以只选择阵列上的特定点打印到文本文件?感谢
import java.io.*;
import java.util.*;
public class Project_Space
{
public static void main(String[] args)
{
// 2D Array of Passengers and Pilots objects- the Passengers class is in the Person.java file
Person[][] Members ;
int num_flights= 6; //create the "x" bound/size of the array
int num_passengers= 9; //create the "y" bound/size of the array
Members = new Person[num_flights][num_passengers];
//The Members array at index 0 is the first company
Members[0][0] = new Person.Flight(1); //This spot in the array is for the Company number in the first spot. Everything else are place holders for data that doesn't pertain to the company
Members[0][1] = new Person.Pilots(1,"1877963200","Amadeus","Durrutti","Buckminster Cornwallis","1288211435", 11); //This spot in the array is for the first team member of company #1
Members[0][2] = new Person.Pilots(2,"6054350085","Sirius","Sassafrass","Ali Bababa","1776812631", 9);
Members[0][3] = new Person.Passengers(1,"7065253333","Amy","Hartman","Betty Sue","7708889999", 3, 50000,"0554055405540554");
Members[0][4] = new Person.Passengers(2,"7545251337","Amanda","Zion","Beatrice Twix","7448656577", 4, 2000,"0554055405541111");
Members[0][5] = new Person.Passengers(3,"8904448899","Janet","Graves","Neal Wade","4445556666", 5, 3000, "9031029266161432");
Members[0][6] = new Person.Passengers(4,"8902234567","Kristen","Pickering","Christopher Soto","5685461208", 6, 51500, "0985028135114275");
Members[0][7] = new Person.Passengers(5,"5000893778","Julianna","Estrada","Jill Hansen","2770779833", 7, 0, "0213595590286251");
Members[0][8] = new Person.Passengers(6,"2080670437","Regena","Mckenzie","Vicki Cook","6224215759", 8, 250, "8204699533830238");
...
Arrays.deepToString(Members);
PrintWriter writer;
try
{
writer = new PrintWriter("flightnames.txt");
for (int i = 0; i<Members.length; i++){
for(int j = 0; j<Members.length; j++){
writer.print(Members[i][j] + ",");
}writer.println();
}}
catch (FileNotFoundException e){
System.out.println("Error: " + e.getMessage());
}
finally{if (writer!=null)
writer.close();
catch (Exception e)
{System.out.println("Could not close writer");}
}
}
}
答案 0 :(得分:0)
移动你的finally块,它应该正确编译。为了将来使用IDE并使用格式化,它会更快地向您显示您的错误:)
Arrays.deepToString(Members);
PrintWriter writer;
try {
writer = new PrintWriter("flightnames.txt");
for (int i = 0; i < Members.length; i++) {
for (int j = 0; j < Members.length; j++) {
writer.print(Members[i][j] + ",");
}
writer.println();
}
} catch (FileNotFoundException ex) {
System.out.println("Error: " + ex.getMessage());
} catch(Exception ex){
System.out.println("Could not close writer"+ ex.getMessage());
} finally {
if (writer != null) {
try{
writer.close();
} catch(IOException ex) {
System.out.print("Error while closing file: "+ ex.getMessage())
}
}
}
答案 1 :(得分:0)
看起来你真正想要的是这个:
...
finally {
if (writer!=null) {
try {
writer.close();
} catch (Exception e) {
System.out.println("Could not close writer");
}
}
}
也就是说,在您的try
电话周围有一个专门的catch
/ writer.close()
来通过关闭writer
来报告错误。
此外,您需要将作者初始化为null
。如果if (writer!=null)
可能永远不会被分配,则您无法writer
(或实际上writer
的任何其他内容)。
因此,请将您的writer
声明更改为:
PrintWriter writer = null;