2013-11-24 171 views
0

我試圖在JOptionPane窗口中顯示搜索結果,以便在文本文件中找到某個搜索結果時,在文本文件中打印出該行。但它在單獨的窗口中打印出每一行,是否有任何方法可以將它們全部打印在同一窗口中?JOptionPane顯示循環

public static void DisplayGrades() throws IOException 
{  
     String line; 
     String stringToSearch = JOptionPane.showInputDialog(null, "Enter your student ID"); 


      BufferedReader in = new BufferedReader(new FileReader("StudentResults.csv")); 
      line = in.readLine(); 

      while (line != null) 
      { 
       if (line.startsWith(stringToSearch)) 
       { 
       JOptionPane.showMessageDialog(null, line);     
       }  
       line = in.readLine(); 
      } 

      in.close(); 



     } 

回答

0

您可以使用StringBuilder所有行追加一個字符串,並把後,纔到JOptionPane

StringBuilder buff = new StringBuilder(); 

BufferedReader in = new BufferedReader(new FileReader("StudentResults.csv")); 
     line = in.readLine(); 

     while (line != null) 
     { 
      if (line.startsWith(stringToSearch)) 
      { 
      buff.append(line).append("\n");     
      }  
      line = in.readLine(); 
     } 

     in.close(); 

     JOptionPane.showMessageDialog(null, buff.toString()); 

作爲一個方面說明

它不是好主意,把CSV成果轉化爲JOptionPane。我會寫定製JDialog