如何读取数字并将数字放在对象数组中

时间:2015-04-02 22:11:45

标签: java arrays file

我有像

这样的file.txt
  

7 10 5
  ADD_FLIGHT SV221 Jeddah NewYork 30 7000
  ADD_FLIGHT SV223 Jeddah London 30 4000
  ADD_FLIGHT SV225 Jeddah Paris 30 3500
  ADD_FLIGHT SV227 Jeddah Cairo 30 2000
  ADD_PASS Mohammed Ali 33 M 0555788778
  ADD_PASS Sara Maghrabi 30 F 0555111111
  ADD_PASS Hani Ali 20 M 0555223344
  ADD_PASS Mohammed Hafeth 33 M 0555889876
  ADD_PASS Ahmad Sami 44 M 0555768768
  ADD_FLIGHT SV332 Jeddah Riyadh 20 500
  ADD_FLIGHT SV334 Jeddah Dammam 20 600
  ADD_FLIGHT SV367吉达迪拜25 2000年   ADD_PASS Salwa Ali 33 F 0555765672
  ADD_PASS Faisal Amri 20 M 0555111111
  ADD_PASS Mona Saleem 33 F 0555222112
  ADD_PASS Ali Ali 33 M 0555743344
  ADD_PASS Marwa Ahmad 33 F 0555545855

我想读取数字7并将数字放在对象数组

我的代码

File fin = new File("input.txt");
FileInputStream fis = new FileInputStream (fin);
BufferedReader br = new BufferedReader (new InputStreamReader (fis));
System.out.println(br.readLine());
String []t=br.readLine().split( "1");
for (int i=0;i<t.length;i++){
    System.out.println(t[i]); 

不正确,因为输出

  

ADD_FLIGHT SV221

我想读取数字7并将数字放在对象数组

请帮助:(

1 个答案:

答案 0 :(得分:1)

我认为你浪费了第一行打印它

//It reads and prints "7 10 5" 
System.out.println(br.readLine());
//Reads "ADD_FLIGHT SV221 Jeddah NewYork 30 7000" and splits it at "1" 
String []t=br.readLine().split( "1"); 
//t contains "ADD_FLIGHT SV22" and " Jeddah NewYork 30 7000"
for (int i=0;i<1;i++){
    //Prints "ADD_FLIGHT SV22"
    System.out.println(t[i]); 
}

你应该:

File fin = new File("input.txt");
FileInputStream fis = new FileInputStream (fin);
BufferedReader br = new BufferedReader (new InputStreamReader (fis));
//Reads first line and split it at every "space"
String []t=br.readLine().split(" "); 
//t contains "7" "10" "5"
System.out.println(t[0]);
//t[0] contains 7, and you can put this value into your array