Java input Five floating-point numbers saved to a file and appended to the file.

Source: Internet
Author: User

Import Java.awt.borderlayout;import java.awt.eventqueue;import Javax.swing.jframe;import Javax.swing.JPanel;import Javax.swing.border.emptyborder;import Java.awt.color;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.io.bufferedreader;import Java.io.datainputstream;import Java.io.dataoutputstream;import Java.io.file;import Java.io.fileinputstream;import java.io.FileNotFoundException; Import Java.io.fileoutputstream;import java.io.filereader;import java.io.filewriter;import java.io.IOException; Import Java.io.printwriter;import java.util.vector;import javax.swing.defaultlistmodel;import Javax.swing.JLabel; Import Javax.swing.joptionpane;import Javax.swing.jscrollpane;import Javax.swing.jtextfield;import Javax.swing.jlist;import Javax.swing.jbutton;import Javax.swing.listmodel;import Java.awt.Font;import Javax.swing.swingconstants;import Javax.swing.jscrollbar;public class Testio extends JFrame implements ActionListener {Private JPanel contentpane;private JTextField number;    private JButton input;    private int num;    Private Defaultlistmodel listmodel,list;    Private Vector<double> V;    Private JList Fudian1;    Private JList fudian2;    Private JButton Anwser;    Private JButton btnsave;       Private double a[]; /** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {testio frame = NE W Testio (); frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the frame. */public Testio () {v=new vector<double> (); Setforeground (Color.green); Settitle ("\u6d6e\u70b9\u6570\u6392\ u5e8f "); Setdefaultcloseoperation (jframe.exit_on_close); SetBounds (+, +), ContentPane = new JPanel (); Contentpane.setborder (New Emptyborder (5, 5, 5, 5)); Setcontentpane (ContentPane); contentpane.setlayout (null); JLabel Lblidhu = new JLabel ("\u6d6e\u70b9\u6570\u4e2a\u6570"); Lblidhu.setbounds (n.; Lblidhu); number = new JTextField (Number.setbounds), Contentpane.add (number), Number.setcolumns (), input = new JButton ("\ Input.setbounds (u8f93\u5165), 201, Input.addactionlistener (this); Contentpane.add (input); Listmodel = new Defaultlistmodel (); list = new Defaultlistmodel (); fudian1 = new JList (Listmodel); fudian1.setvalueisadjusting (true); JScrollPane j = new JScrollPane (fudian1) j.setbounds (155,, 133); Contentpane.add (j); fudian2 = new JList (list); JScrollPane J2 = new JScrollPane (fudian2), j2.setbounds (284,, 133); Contentpane.add (J2); anwser = new JButton ("\ Anwser.setbounds (U6253\U5F00); Anwser.addactionlistener (this); Contentpane.add (anwser); btnsave =-- New JButton ("\u4fdd\u5b58."); Btnsave.setbounds (257, ten, Btnsave.addactionlistener); Contentpane.add (btnsave); JLabel label = new JLabel ("\u6392\u5e8f\u4e4b\u524d"); Label.setbounds (156,, si); contentpane.add (label); JLabel label_1 = new JLabel ("\u6392\u5e8f\u4e4b\u540e"); Label_1.setbounds (2(Contentpane.add); (Label_1); JLabel Lbltip = new JLabel ("Tip\uff1a\u8bf7\u5148\u8f93\u5165\u6d6e\u70b9\u6570\u4e2a\u6570\uff0c\u6216\u8005\ U6253\u5f00\u6587\u4ef6 "), Lbltip.setverticalalignment (Swingconstants.top), Lbltip.setfont (New Font (" Microsoft Ya Black ", Lbltip.setbounds (Font.plain, 234, +); Contentpane.add (Lbltip);} @Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method Stubif (E.getsource () ==input) { Listmodel.removeallelements (); string s = Number.gettext (); num = Integer.parseint (s); for (int i=1;i<=num;i++) {String name= "Please enter the number of" +i+ "; String ss= Joptionpane.showinputdialog (name); Listmodel.addelement (double.parsedouble (ss)); V.add ( Double.parsedouble (ss));}} if (E.getsource () ==btnsave) {Save ();} if (E.getsource () ==anwser) {v.removeallelements (); String name= "Please enter the document path ' C:\txt '"; String ss= Joptionpane.showinputdialog (name);        File File = new file (ss);            try {FileReader fr = new FileReader (file); BufferedReader br = new BufferedrEader (FR);                Try {string s, s2 = new String ();                    while ((s = br.readline ()) = null) {s2 + = s + "\ n";                V.add (double.parsedouble (s));                } br.close ();                SYSTEM.OUT.PRINTLN (S2);                System.out.println ("OK");                for (int i=0;i<v.size (); i++) {System.out.println (V.elementat (i)); }} catch (IOException E1) {//TODO auto-generated catch block E1.printstacktrac            E (); }} catch (FileNotFoundException E1) {//TODO auto-generated catch block E1.printstacktrace (        );        } int i,j;                 Double temp;                Num=v.size ();                String g = "" +num;                Number.settext (g);                A=new Double[num];                Listmodel.removeallelements (); for (i=0;i<num;i++) {a[i]=V.elementat (i);                Listmodel.addelement (A[i]);         } for (i=0;i<num-1;i++) for (j=i+1;j<num;j++) if (A[i]>a[j]) {temp=a[i];         A[I]=A[J];         A[j]=temp;        } v.removeallelements ();        for (i=num-1;i>=0;i--) {System.out.println (a[i]);        List.addelement (A[i]);        V.addelement (A[i]);                  } try {FileWriter writer = new FileWriter (ss, True); For (I=0;i<v.size (); i++) {double K=v.elementat (i);            String sss = k + "\ r \ n";             Writer.write (SSS);            System.out.println (k);              } writer.close ();              } catch (IOException E1) {e1.printstacktrace ();          }/* PrintWriter PW = null;              try {pw = new PrintWriter (new FileWriter (ss));            For (I=0;i<v.size (); i++) {double K=v.elementat (i);        Pw.print (k + "\ r \ n");      System.out.println (k);          }} catch (FileNotFoundException E1) {e1.printstacktrace ();          } catch (IOException E1) {e1.printstacktrace ();                  }finally {if (PW = null) {pw.close ();              PW = NULL; }} */}}public void Save () {String name= "Please enter save document path ' C:\txt '"; String ss= Joptionpane.showinputdialog (name);          PrintWriter pw = null;              try {pw = new PrintWriter (new FileWriter (ss));            for (int i=0;i<v.size (); i++) {double K=v.elementat (i);              Pw.print (k + "\ r \ n");            System.out.println (k);          }} catch (FileNotFoundException E1) {e1.printstacktrace ();          } catch (IOException E1) {e1.printstacktrace ();                  }finally {if (PW = null) {pw.close ();              PW = NULL; }          }}}

Java input Five floating-point numbers saved to a file and appended to the file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.