Join 86,253 Java Programmers. There are 2,125 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
My algorithm looks like this: start with a welcome screen, prompting to create a new account or search previous. if the new account button is pressed, a new window is opened prompting user to display all information. etc...
public NewAccount() { JFrame acctFrame = new JFrame("Personal Information"); JPanel acctPane = new JPanel(); acctPane.setLayout(new GridLayout(14,2));
JLabel first = new JLabel(" First Name:"); JLabel last = new JLabel(" Last Name:"); JLabel middle = new JLabel(" Middle Name:"); JLabel type = new JLabel(" Gender:"); String [] combopkg = {"Male","Female"}; JComboBox gender = new JComboBox(combopkg); ............labels for the address and such............. JLabel password = new JLabel(" Password");
JTextField fName = new JTextField(15); // this is what i tried but it still doesn't work String fieldText = fName.getText(); ............text fields for the address and such............. JPasswordField pf = new JPasswordField(15); JTextField cphone = new JTextField(10);
JButton create = new JButton("Create"); JButton cancel = new JButton("Cancel"); acctPane.add(first); acctPane.add(fName); acctPane.add(last); acctPane.add(lName); ........the rest of the acctPane.add()...................... acctPane.add(cancel); acctPane.add(create);
JLogin login = new JLogin(); JLogin1 login1 = new JLogin1(); cancel.addActionListener(login); create.addActionListener(login1);
class JLogin implements ActionListener { public void actionPerformed(ActionEvent cancel) { Object ob = cancel.getSource(); if (ob == cancel) { Welcome w = new Welcome(); } } }
String name; class JLogin1 implements ActionListener { public void actionPerformed(ActionEvent create) { // this is where my code faults. // it's not taking the values from what the user enters name = (fieldText.getText()); Object ob = create.getSource(); if (ob == create) { try { PrintWriter outFile = new PrintWriter ("info.txt"); outFile.println("Name: " + name); } } } } }
I realize that I'm not finished writing the code to save the info, but I haven't been able to figure out how to get the program to read the values the user enters in those fields for about 5 hours now lol so i figured i'd just post this
My algorithm looks like this: start with a welcome screen, prompting to create a new account or search previous. if the new account button is pressed, a new window is opened prompting user to display all information. etc...
i can't figure out how to save this information?
Just a little little misconcept... I think
java
public class NewAccount extends JFrame {
public NewAccount() {
JTextField fName = new JTextField(15); // this is what i tried but it still doesn't work // If you think that means whenever fName changes the result will be put in fieldText // you are wrong // this takes what is in fName NOW and put it in fieldText // as fName hasn't been populated at this moment fieldText == "" // so this statement is useless String fieldText = fName.getText(); ............text fields for the address and such............. JPasswordField pf = new JPasswordField(15); }
String name; class JLogin1 implements ActionListener { public void actionPerformed(ActionEvent create) { // this is where my code faults. // it's not taking the values from what the user enters // there is still no values in fieldText but now you want to copy what is in fName /// name = (fieldText.getText()); name = (fName.getText()); Object ob = create.getSource(); if (ob == create) { try { PrintWriter outFile = new PrintWriter ("info.txt"); outFile.println("Name: " + name); } } } } }
It's still not working, I'm using Eclilpse and it's telling me that the field cannot be resolved...
java
public class NewAccount extends JFrame {
public NewAccount() { JTextField fName = new JTextField(15); /* here it's saying that fieldText is never read */ String fieldText = fName.getText();
JLogin login = new JLogin(); JLogin1 login1 = new JLogin1(); cancel.addActionListener(login); create.addActionListener(login1); } class JLogin1 implements ActionListener { public void actionPerformed(ActionEvent event) { String fullName; String fullAddress; String phoneNumbers; File tmpFile; File backupFile; File file; /* and here it's saying that it can't be resolved */ fullName = fieldText.getText();
literally, all I want to do is take what the user enters for his name (first middle and last) address and phone number and store it in a file so I can recall it with a search method and display it as a list.....and I'm soooo lost
This post has been edited by iNaStY v3: 8 May, 2008 - 12:32 PM
It's still not working, I'm using Eclilpse and it's telling me that the field cannot be resolved...
java
public class NewAccount extends JFrame { // fName should be declared here as an instance variable JTextField fName = new JTextField(15);
// now your constructor public NewAccount() { /* here it's saying that fieldText is never read */ String fieldText = fName.getText(); // Eclipse is right... your are putting the content of the JTextField in fieldText // but you never use fieldText after so Eclipse tells you that you are performing a useless operation
JLogin login = new JLogin(); JLogin1 login1 = new JLogin1(); cancel.addActionListener(login); create.addActionListener(login1); } class JLogin1 implements ActionListener { public void actionPerformed(ActionEvent event) { String fullName; String fullAddress; String phoneNumbers; File tmpFile; File backupFile; File file; /* and here it's saying that it can't be resolved */ // sure fieldText is defined in your constructor JLogin1 cannot see it fullName = fieldText.getText(); // you should do fullName = fName.getText()
literally, all I want to do is take what the user enters for his name (first middle and last) address and phone number and store it in a file so I can recall it with a search method and display it as a list.....and I'm soooo lost
Ohhhhh that makes perfect sense. I think I tried that earlier, but when I did I think I changed everything to static (for some strange reason lol) and obviously it didn't work.
making those changes you suggested, now it won't write it to a file at all, and I'm getting a different error message
java
class JLogin1 implements ActionListener { public void actionPerformed(ActionEvent event) { String fullName; String fullAddress; String phoneNumbers; File tmpFile; File backupFile; File file; fullName = lName.getText() + ", " + fName.getText() + ", " + mName.getText(); fullAddress = add.getText() + "\n" + cit.getText() + ", " + st.getText() + " " + zipCode.getText(); phoneNumbers = "Home: " + hphone.getText() + "\nWork: " + wphone.getText() + "\nCell: " + cphone.getText(); /* Used a try/catch loop to first write the file as a temp * then used FilSaver.java constructor to save as .bak (backup) * This is actually a snipet I found online * but then here it says that at FileSaver(file) that String cannot be converted to a File * As it is, it's saying that the local variable file hasn't been initialized */ try { FileSaver saver = new FileSaver(file); final Writer writer = saver.getWriter(); PrintWriter out = new PrintWriter(writer); out.close(); saver.finish(); System.out.println("Saved OK"); } catch (IOException e) { System.out.println("Save FAILED"); // exception thrown }
}
I've been searching online, and all I've found is long long long snipets that I can't implement.
I've also tried to use FileWriter, but that also doesn't work with a String.
Well right now as it is (the code from above) I'm getting the error message at
java
FileSaver saver = new FileSaver(file);
at (file) --- it's saying that the local variable may not have been initialized.
if I declare
java
File tmpFile; File backupFile; File file;
before my constructor like before, that error message goes away, but when I run the program, if I click on create, nothing happens (I know I haven't told it what to do) but no file gets created -- at least I don't think it does, because saving it like this I have no idea where it goes, or what it's name is, or MOST importantly how to call it....I've tried calling it with FileReader, but I can't figure out what it's called
I have no idea what that means pbl lol sorry. Let me rephrase:
As my code is, it tells me that (file) hasn't be initialized. So if I declare it before my constructor, that error message goes away, but when I run the program, if I click on create, nothing happens (I know I haven't told it what to do) but no file gets created -- at least I don't think it does, because saving it like this I have no idea where it goes, or what it's name is, or MOST importantly how to call it....I've tried calling it with FileReader, but I can't figure out what it's called
Yes that part I've gotten, but that created two more problems ( )
first of all, it's creating the file, but it's not writing anything to it, so I added to that (my code is below with comments) but this starts my second, more important problem.
doing it this way writes a new file everytime, and they'll all be called the same thing (and I think I need everything saved in one place)
Bottom line is I need to be able to search my customer's by their name, and have my program output their name, address, and phone numbers in a String (and then later I'll add accounts) and with the code written this way I have no idea how to do that.... I just need a starting point
Sorry I'm not explaining this very well but I hope I cleared it up. Thanks for the help
java
/* for the file name, is it possible to save it as the persons name?? */ file = new File("Name.dat"); FileSaver saver = new FileSaver(file); final Writer writer = saver.getWriter(); PrintWriter out = new PrintWriter(writer); /* this is what I added to write the file, but I get the error message * The method myWriteOutputFile(PrintWriter) is undefined for the type * NewAccount.JLogin1 */ myWriteOutputFile(out); out.close(); saver.finish(); System.out.println("Saved OK");
inputFile = new File(toString); try { File inputFile = (FileSaver.getFile()); inputFile = new File("tmpFile"); FileSaver saver = new FileSaver(inputFile); final Writer writer = saver.getWriter(); Writer out = new FileWriter(tmpFile); out.close(); saver.finish();
in the folder, it saves tmpFile.bak and tmpFile.tmp, but it doesn't write anything into it.
Here's the methods from my class FileSave
java
public FileSaver(File input) throws IOException {
// Step 1: Create temp file in right place this.inputFile = input; tmpFile = new File(inputFile.getAbsolutePath() + ".tmp"); tmpFile.createNewFile(); tmpFile.deleteOnExit(); backupFile = new File(inputFile.getAbsolutePath() + ".bak"); state = State.AVAILABLE; }
public static File getFile() { return inputFile; }
public Writer getWriter() throws IOException {
if (state != State.AVAILABLE) { throw new IllegalStateException("FileSaver not opened"); } Writer out = new FileWriter(tmpFile); state = State.INUSE; return out; }
public void finish() throws IOException {
if (state != State.INUSE) { throw new IllegalStateException("FileSaver not in use"); }
// Delete the previous backup file if it exists; backupFile.delete();
// Rename the user's previous file to itsName.bak, // UNLESS this is a new file ; if (inputFile.exists() && !inputFile.renameTo(backupFile)) { throw new IOException("Could not rename file to backup file"); }
// Rename the temporary file to the save file. if (!tmpFile.renameTo(inputFile)) { throw new IOException("Could not rename temp file to save file"); } state = State.AVAILABLE; } }