Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

From textboxes to text file

Hi

Just want help in printing values from textboxes into a text file. Its a simple programme in which a user enters details about themselves ie.Name and when they click the enter button, the information entered into the textboxes is inputted into a text file.
If there is anyone out there who would be able to code this it would be greatly appreciated.

Thanks in advance.
0
rebel_vb
Asked:
rebel_vb
2 Solutions
 
mitchweb1Commented:
Here are the parts that should help you a bit with your problem, just fit them in the right place in your code, you need to import the javax.swing to get the option panel to work. hope this helps

import javax.swing.*;
//TO GET INFORMATION
String <VARIABLE> = JOptionPane.showInputDialog(null, "Enter Integer");



//TO WRITE TO FILE

try {
                    BufferedWriter buffer = new BufferedWriter(new FileWriter(<YOUR FILENAME>, true));
                    buffer.newLine();
                    buffer.write(<VARIABLE NAME>);
                    buffer.close();
                } catch (IOException e) {
                }
                    System.exit(0);
          }
};
0
 
mitchweb1Commented:
forgot to mention, the way that code is written means it will append to a file of your choice, on a new line... presuming thats what you want it to do?
0
 
FaithNoMoreCommented:
If the information from the text boxes make a object you can use writeObject();  I am currently at work, so my syntax may be a little off.

You may have problems reading in the objects after they have been saved (ie. ClassNotFoundException) which can be fixed with a little work.



import java.io.*;

public class Person implements Serializable
{
    String name;
    int age;
    // ..etc

   public Person(String name, int age)
   {
     this.name = name;
     this.age = age;
   
   }


//Method takes in a Person object and writes it to
//the PersonFile.txt.  If the file doesnt exsist it
//will be created.
  static void SavePerson( Person person )
   {
      File file = new File("personFile.txt");
      ObjectOutputStream out;
      try{
      out = new ObjectOutputStream(
                  new FileOutputStream(file));
      writeObject(person);
      out.close()
      }catch(IOException err){
          err.printStackTrace();
      }
   }

}

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
FaithNoMoreCommented:
Also, to get the info from the text fieldds use:

TextField Name = new TextField();
String name;
TextField Age = new TextField();
int age;

name = Name.getText().trim();
double temp = new Double(Age.getText().trim());
age = temp.intValue();  //I think this will work?

Person p = new Person(name, age);
Person.SavePerson(p);

0
 
rebel_vbAuthor Commented:
Just like to thank ye all for the help. Much appreciated. After posting 2 more questions, again any help is appreciated. Thanks again.
0
 
CleanupPingCommented:
rebel_vb:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
jimmackCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split between mitchweb1 and FaithNoMore

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now