2013-03-10 56 views
0

想要打印出存儲的信息。我該如何解決這個問題?試圖讀取文本文件,將其存儲到arrayList中,並將存儲的信息打印到屏幕上。爲什麼我的輸出是空的?

public class schoolTimeTable { 

    private static ArrayList<String> timesArray = new ArrayList<String>(); 


    public static void main(String[] args) { 

     Scanner scanner = null; 

     try { 


     File file = new File("C:/Users/Tommy/workspace/Prov/src/TestPaket/text.txt"); 
     scanner = new Scanner(file); 
     while(scanner.hasNextLine()){ 

      String[] tokens = scanner.nextLine().split("\\s+"); 

      String[] times = tokens; 
      for(String time: times) 
      timesArray.add(time); 
      System.out.println(timesArray); 



     }} catch (Exception e) { 
     // TODO: handle exception 
    } 

}} 
+0

「打印」一個ArrayList不會(通常)打印數組列表的內容,但是ArrayList#toString想要打印什麼,這通常是對象hashCode – MadProgrammer 2013-03-10 03:02:22

+0

我打賭一個異常被拋出,並且由於你不在catch塊中打印棧跟蹤,所以你什麼都看不到。永遠不要做一個空的catch塊。 – 2013-03-10 03:02:33

+0

@MadProgrammer AbstractCollection(其中ArrayList是一個子類)重新定義toString(),以便打印括在方括號中的集合元素。這是記錄,所以我想可以依靠這個。 – 2013-03-10 03:04:26

回答

1

由於沒有打印任何內容,我的假設是文件名不正確,或者Java不喜歡它。

仔細檢查路徑,並嘗試更換向前(雙)反斜槓,像這樣斜線:

C:\\Users\\Tommy\\workspace\\Prov\\src\\TestPaket\\text.txt 

,當然,正如其他人的說,你catch塊更改爲:

catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

斜線應該沒有區別(驗證路徑也是一個好主意) - 但打印例外是一個好主意。 – MadProgrammer 2013-03-10 03:46:47

4

如果你更加關注代碼格式和風格,你會做得更好。這對於可讀性和理解都很重要。

catch塊什麼都不做。你永遠不應該有一個空的catch塊。總是至少打印堆棧跟蹤。

正常工作:

package cruft; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

/** 
* SchoolTimeTable 
* @author Michael 
* @link http://stackoverflow.com/questions/15318400/trying-to-read-from-a-text-file-store-it-into-an-an-arraylist-and-print-the-sto/15318413#15318413 
* @since 3/9/13 10:02 PM 
*/ 
public class SchoolTimeTable { 

    private static List<String> timesArray = new ArrayList<String>(); 


    public static void main(String[] args) { 
     try { 
      File file = new File("resources/timeTable.txt"); 
      Scanner scanner = new Scanner(file); 
      while (scanner.hasNextLine()) { 
       String[] times = scanner.nextLine().split("\\s+"); 
       for (String time : times) { 
        timesArray.add(time); 
       } 
       System.out.println(timesArray); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

我把這個timeTable.txt在資源文件中我的項目:

1 
2 3 4 
5 6 7 8 

這裏是我,當我跑它的輸出:

"C:\Program Files\Java\jdk1.7.0_17\bin\java" cruft.SchoolTimeTable 
[1] 
[1, 2, 3, 4] 
[1, 2, 3, 4, 5, 6, 7, 8] 

Process finished with exit code 0 

這看起來很適合我。

+0

無法想象爲什麼有人會爲此付出代價。這個答案究竟有什麼錯誤? – duffymo 2013-03-10 03:08:50

0

也是讀取文件? 我運行你的代碼。是的。 文件的內容是:

demo1,28,FEB-01,真正的 demo2,22,DEC-03,假 demo3,21,DEC-03,假 demo4,25,DEC-03,真正

文件名:Visitor.txt 我改變的代碼一點點:

package test; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class schoolTimeTable { 
    private static ArrayList<String> timesArray = new ArrayList<String>(); 
    public static void main(String[] args) { 
     Scanner scanner = null; 
     try { 
      File file = new File("d:/Visitor.txt"); 
      scanner = new Scanner(file); 
      while (scanner.hasNextLine()) { 
       String[] tokens = scanner.nextLine().split("\\s+"); 
       String[] times = tokens; 
       for (String time : times) 
        timesArray.add(time); 
      } 
      System.out.println(timesArray); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

輸出: [demo1,28,FEB-01,真實,demo2,22,DEC-03 ,false,demo3,21,dec-03,false,demo4,25,dec-03,true]。

在我看來你的文件格式的內容。 您比較文件格式的內容。 希望能夠幫助

相關問題