JOptionPane显示循环 [英] JOptionPane display loop

查看:190
本文介绍了JOptionPane显示循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在JOptionPane窗口中显示搜索结果,以便在文本文件中找到特定搜索时将其打印出文本文件中的行.但是它在单独的窗口中打印出每一行,有什么办法可以在同一个窗口中将它们全部打印出来?

Im trying to display the results of a search in a JOptionPane window so that when a certain search is found in a text file that it prints out the line in the text file. But it prints out every line in a separate window , is there any way I could print them all in the same window ??

 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();  



      }

推荐答案

您可以使用StringBuilder将所有行追加到一个String中,并且仅在将其放入JOptionPane之后.

You can use StringBuilder to append all lines in one String and only after put it to 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

Its not good idea to put csv results into JOptionPane. I would write custom JDialog

这篇关于JOptionPane显示循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆