JOptionPane display loop

北城以北 提交于 2019-12-11 14:12:57

问题


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



      }

回答1:


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

As a side note:

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



来源:https://stackoverflow.com/questions/20180567/joptionpane-display-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!