在一个表单里提交带数字和逗号的字符串,在后台解析这个字符串,提取出数字并存放在一个数组或者集合中,同时进行排序!再将这个数组转成字符串形式,传回JSP!哈哈!

解决方案 »

  1.   

    很简单,用token取出所有数字
      public static String[] splitStringByComma(String source){
      if(source==null||source.trim().equals(""))
    return null;
      StringTokenizer commaToker =  new StringTokenizer(source,",");
      String[] result = new String[commaToker.countTokens()];
      int i=0;
      while(commaToker.hasMoreTokens()){
    result[i] = commaToker.nextToken();
    i++;
      }
      return result;
      }
    循环遍历String数组
    Integer.parseInt(String s)变成int类型
    组成int数组
    Arrays.sort(int[] a),
    a数组升序
    降序可以从尾部开始输出
      

  2.   

    不用jsp。用java application!做!
      

  3.   

    自己画画界面 写一下按钮触发事件
    然后调用上面: gdsean(摇滚java) 的方法
    不就可以!reset把界面还原!
      

  4.   

    reset 就把TextArea清空! :)
      

  5.   

    package study1;import javax.swing.UIManager;
    import java.awt.*;public class Application1 {
      boolean packFrame = false;  /**Construct the application*/
      public Application1() {
        Frame1 frame = new Frame1();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame) {
          frame.pack();
        }
        else {
          frame.validate();
        }
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
      }
      /**Main method*/
      public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
          e.printStackTrace();
        }
        new Application1();
      }
    }package study1;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class Frame1 extends JFrame {
      JPanel contentPane;
      JTextField jTextField1 = new JTextField();
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      int[] a;
      JTextArea jTextArea1 = new JTextArea();
      JCheckBox jCheckBox1 = new JCheckBox();
      /**Construct the frame*/
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception  {
        //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(gridBagLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        jButton1.setText("ok");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            jButton1_mouseClicked(e);
          }
        });
        jButton2.setText("reset");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton2_actionPerformed(e);
          }
        });
        jCheckBox1.setText("desc");
        contentPane.add(jButton1,           new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0
                ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 0, 3), 91, 32));
        contentPane.add(jTextField1,            new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
                ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 198, 32));
        contentPane.add(jTextArea1,                 new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0
                ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(12, 0, 14, 0), 205, 44));
        contentPane.add(jButton2,      new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(9, 0, 9, 3), 97, 24));
        contentPane.add(jCheckBox1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
      }
      /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  void jButton1_mouseClicked(MouseEvent e) {
          a=splitStringByComma(this.jTextField1.getText());
          Arrays.sort(a);
          this.jTextArea1.setText("");
          if (this.jCheckBox1.isSelected()){
            for(int i=a.length-1;i>=0;i--){
              this.jTextArea1.append(String.valueOf(a[i]));
            }
          }else{
            for(int i=0;i<a.length;i++){
              this.jTextArea1.append(String.valueOf(a[i]));
            }      }
      }
      public int[] splitStringByComma(String source){
      if(source==null||source.trim().equals(""))
    return null;
      StringTokenizer commaToker =  new StringTokenizer(source,",");
      int[] result = new int[commaToker.countTokens()];
      int i=0;
      while(commaToker.hasMoreTokens()){
                  try{
    result[i] =Integer.parseInt(commaToker.nextToken());
    i++;
                  }catch(Exception e){}
      }
      return result;
      }  void jButton2_actionPerformed(ActionEvent e) {
          this.jTextArea1.setText("");
          this.jTextField1.setText("");
      }
    }
      

  6.   

    xmvigour(微电--电力设备遭受重创,暂时停止放电) (  ) 
    不过我的要求是在另外一个窗体显示结果!
      

  7.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class Frame1 extends JFrame {
      JPanel contentPane;
      JTextField jTextField1 = new JTextField();
      JButton jButton1 = new JButton();
      JButton jButton2 = new JButton();
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      int[] a;
      JTextArea jTextArea1 = new JTextArea();
      JCheckBox jCheckBox1 = new JCheckBox();
      Frame2 from2=new Frame2(this);
      /**Construct the frame*/
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception  {
        //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(gridBagLayout1);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        jButton1.setText("ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
          }
        });
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            jButton1_mouseClicked(e);
          }
        });
        jButton2.setText("reset");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton2_actionPerformed(e);
          }
        });
        jCheckBox1.setText("desc");
        contentPane.add(jButton1,           new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0
                ,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 5, 0, 3), 91, 32));
        contentPane.add(jTextField1,            new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
                ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 198, 32));
        contentPane.add(jTextArea1,                 new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0
                ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(12, 0, 14, 0), 205, 44));
        contentPane.add(jButton2,      new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(9, 0, 9, 3), 97, 24));
        contentPane.add(jCheckBox1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
      }
      /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  void jButton1_mouseClicked(MouseEvent e) {
          a=splitStringByComma(this.jTextField1.getText());
          Arrays.sort(a);
          this.jTextArea1.setText("");
          if (this.jCheckBox1.isSelected()){
            for(int i=a.length-1;i>=0;i--){
              this.jTextArea1.append(String.valueOf(a[i]));
            }
          }else{
            for(int i=0;i<a.length;i++){
              this.jTextArea1.append(String.valueOf(a[i]));
            }      }
          from2.setText1(this.jTextArea1.getText());
          from2.show();
      }
      public int[] splitStringByComma(String source){
      if(source==null||source.trim().equals(""))
    return null;
      StringTokenizer commaToker =  new StringTokenizer(source,",");
      int[] result = new int[commaToker.countTokens()];
      int i=0;
      while(commaToker.hasMoreTokens()){
                  try{
    result[i] =Integer.parseInt(commaToker.nextToken());
    i++;
                  }catch(Exception e){}
      }
      return result;
      }  void jButton2_actionPerformed(ActionEvent e) {
          this.jTextArea1.setText("");
          this.jTextField1.setText("");
      }  void jButton1_actionPerformed(ActionEvent e) {  }
    }package study1;import java.awt.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;
    import java.awt.event.*;/**
     * Title:        study
     * Description:
     * Copyright:    Copyright (c) 2001
     * Company:      lwz
     * @author
     * @version 1.0
     */public class Frame2 extends JFrame {
      JTextArea jTextArea1 = new JTextArea();
      GridLayout gridLayout1 = new GridLayout();
      JPanel jPanel1 = new JPanel();
      JButton jButton1 = new JButton();
      GridBagLayout gridBagLayout1 = new GridBagLayout();
      Frame1 frame;
      public Frame2(Frame1 frame1) {
        this.frame=frame1;
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
        this.jTextArea1.setText(this.frame.jTextArea1.getText());
      }
      public Frame2() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        jTextArea1.setText("jTextArea1");
        this.getContentPane().setLayout(gridLayout1);
        jPanel1.setLayout(gridBagLayout1);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
          }
        });
        this.getContentPane().add(jPanel1, null);
        jPanel1.add(jTextArea1,     new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
                ,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 340, 88));
        jPanel1.add(jButton1,              new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 88, 24));
      }  void jButton1_actionPerformed(ActionEvent e) {
          this.frame.show();
      }
      public void setText1(String s){
        this.jTextArea1.setText(s);
      }
    }