Results 1 to 19 of 19
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default Io trouble need help

    Hi I am running through this program and I am getting no errors but the problem is the output to my text file. When I click register the keys/values in the JTextFields should get written to the txt file "users.txt". The program works well the first time I start it up but when I start it again and send the keys/values to the file the same key and value from the first time pop up and so forth through each iteration of my program.

    Java Code:
    package piced;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    import java.util.Hashtable;
    
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class Register {
    
    	JFrame frame = new JFrame("Registration");
    	JPanel panel = new JPanel();
    	JButton exit = new JButton("Exit");
    	JButton regi = new JButton("Register");
    	JLabel lab1 = new JLabel("Enter Name: ");
    	JTextField name1 = new JTextField();
    	JLabel lab2 = new JLabel("Confirm Name: ");
    	JTextField name2 = new JTextField();
    	JLabel lab3 = new JLabel("Enter Password: ");
    	JTextField pass1 = new JTextField();
    	JLabel lab4 = new JLabel("Confirm Password: ");
    	JTextField pass2 = new JTextField();
    	static Hashtable<String, String> users = new Hashtable<String, String>();
    	static File file = new File("/Users/eric.manget/Desktop/users.txt");
    
    	public Register() {
    
    		GridLayout lay = new GridLayout(6, 1);
    
    		panel.setLayout(lay);
    
    		panel.add(lab1);
    		panel.add(name1);
    		panel.add(lab2);
    		panel.add(name2);
    		panel.add(lab3);
    		panel.add(pass1);
    		panel.add(lab4);
    		panel.add(pass2);
    		panel.add(new JLabel(""));
    		panel.add(new JLabel(""));
    		panel.add(regi);
    		panel.add(exit);
    		frame.add(panel);
    		read();
    		frame.setSize(280, 200);
    		frame.setResizable(false);
    		frame.setVisible(true);
    
    		exit.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    
    				frame.dispose();
    
    			}
    		});
    
    		regi.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    
    				if (name1.getText().equals(name2.getText())) {
    					if (pass1.getText().equals(pass2.getText())) {
    
    						String key = name1.getText();
    						String value = pass1.getText();
    
    						if (users.containsKey(key) == false) {
    
    							if (key.equals("") == false) {
    								if (value.equals("") == false) {
    
    									users.put(key, value);
    									Confirm("CONFIRM: If you continue program will close.",
    											310, 80);
    
    								} else {
    
    									Error("ERROR: Please write a password.",
    											330, 50);
    
    								}
    
    							} else {
    
    								Error("ERROR: Please write a name.", 330, 50);
    
    							}
    
    						} else {
    
    							Error("ERROR: There is already a user with that name.",
    									330, 50);
    
    						}
    					} else {
    
    						Error("ERROR: Passwords are not equal to each other.",
    								320, 50);
    
    					}
    
    				} else {
    
    					Error("ERROR: Names are not equal to each other.", 300, 50);
    
    				}
    
    			}
    		});
    
    	}
    
    	public void Error(String error, int width, int height) {
    
    		JFrame err1 = new JFrame("Error");
    		JPanel perr = new JPanel();
    
    		perr.add(new JLabel(error));
    		err1.add(perr);
    
    		err1.setResizable(false);
    		err1.setSize(width, height);
    		err1.setVisible(true);
    
    	}
    
    	public void Confirm(String confirm, int width, int height) {
    
    		final JFrame err1 = new JFrame("Error");
    		JPanel perr = new JPanel();
    		JButton yes = new JButton("Continue");
    		JButton no = new JButton("Back");
    
    		perr.add(new JLabel(confirm));
    		perr.add(yes);
    		perr.add(no);
    		err1.add(perr);
    		err1.setResizable(false);
    		err1.setSize(width, height);
    		err1.setVisible(true);
    
    		yes.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    
    				write();
    				System.exit(0);
    
    			}
    		});
    
    		no.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    
    				err1.dispose();
    
    			}
    		});
    
    	}
    
    	public void write() {
    
    		try {
    
    			FileWriter outFile = new FileWriter(file, true);
    			PrintWriter out = new PrintWriter(outFile);
    
    			Enumeration k = users.keys();
    			while (k.hasMoreElements()) {
    
    				String key = (String) k.nextElement();
    				String output = key + ";" + users.get(key) + "\n";
    
    				out.write(output);
    				out.close();
    
    			}
    
    		} catch (IOException e) {
    
    			Error("ERROR: User database not found.", 300, 46);
    
    		}
    
    	}
    
    	public void read() {
    
    		try {
    
    			BufferedReader in = new BufferedReader(new FileReader(file));
    			String str;
    
    			String str1;
    			String str2;
    			users = new Hashtable<String, String>();
    			while ((str = in.readLine()) != null) {
    				int k = str.indexOf(';');
    				if (k > 0) {
    
    					str1 = str.substring(0, k);
    					str2 = str.substring(k + 1);
    
    					users.put(str1, str2);
    				}
    
    			}
    
    		} catch (Exception e) {
    
    			Error("ERROR: Could not read from users database.", 300, 60);
    
    		}
    
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    but when I start it again and send the keys/values to the file the same key and value from the first time pop up and so forth through each iteration of my program.
    You need to explain what should happen.

    What do you mean by "pop up"?

    The program has no comments or help, so I have no idea how to use it.
    I enter data and press buttons and what is supposed to happen?
    Last edited by Norm; 06-02-2011 at 10:50 PM.

  3. #3
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Hi Norm,

    Sorry for the late response but for some odd reason the problem fixed itself but now a new problem has arisen
    the code to check if there already is a key in the users.txt file is not working as it did before. I have made no changes.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    now the text in the jtextfields are the same each time I start the program
    I'd suggest you look at all the places where the text is set. Add some printlns to show what/where/how the textfields are being set. Something in the printouts should show you what is happening.

  5. #5
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok my last solution was wrong so I put the == false back in to correct it. The problem is the read method I think. When regi is pressed the program isn't recognizing the keys in the hashtable thereby allowing keys to be printed into it again and again.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    the program isn't recognizing the keys in the hashtable
    Same advice:
    Add some printlns to show what/where/how the variables are being set.

    Can you show the code?

  7. #7
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    I gave you my code. What do you want specifically? A screenshot of my txt file or my other classes which are in my opinion irrelevant to this registration form other then the calling of it.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    The code had bugs in it that I thought you had changed.
    Explain exactly what happens.
    Show the contents of the file that is read.
    What buttons are pressed, what text is entered and what happens.
    Step by step by step.

    What did you see when you:
    Added some printlns to show what/where/how the variables are being set.
    I need your code that has these printlns in it. I need to see how you have tried to debug the code. Perhaps I can make some recommendations on ways to do it.

  9. #9
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok, I placed some printlns around and found that the output from write is correct and the Strings I want to be read are correct. Now I know the problem is the hashtable doesn't have the keys when it is searched for a specific key. It has the keys if the program is running and I keep clicking regi so the user is put in the hashtable but when I quit the program and launch it again it doesn't seem to have the key in it's possession.

  10. #10
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Do you mind if I not post the code you asked for because it is irrelevant now that we have cornered the problem.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    when I quit the program and launch it again it doesn't seem to have the key in it's possession.
    Absolutely. You'll have to read them in and fill the hashtable before going on.

  12. #12
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok, so now the hashtable has the keys but it is odd what happens when I write. I just wrote six user keys/values to my txt file and then I looked in it. It wrote the users until the fifth and sixth times. At those points it just wrote what was written in the fourth entry.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    Continue adding printlns until you see what is happening.
    Check the code in the write method.

  14. #14
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok I have discovered it is not set to repeat at the fourth time. It repeats randomly.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    As I said add more printlns at the places where you need to see what is happening and look at the output.

  16. #16
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    I put a println right before the program writes to the file and it states the key/value I want but what it writes is a repeat (at random times).

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    Show the output.

    Look at the write method.
    Why do you close the file after every write?
    Do it once after all the writes.

  18. #18
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    The file only closes when my write method is executed now.

    The write method with the printlns:

    Java Code:
    public void write() {
    
    		try {
    
    			FileWriter outFile = new FileWriter(file, true);
    			PrintWriter out = new PrintWriter(outFile);
    
    			Enumeration k = users.keys();
    			while (k.hasMoreElements()) {
    
    				String key2 = (String) k.nextElement();
    				String output = key2 + ";" + users.get(key2) + "\n";
    
    				System.out.println(output);
    				out.write(output);
    				System.out.println(output);
    				out.close();
    
    			}
    
    		} catch (IOException e) {
    
    			Error("ERROR: User database not found.", 300, 46);
    
    		}
    
    	}
    The output:

    Java Code:
    Mant;lon
    
    Mant;lon
    
    Mind;freak
    
    Mind;freak

    What is on the users.txt file:


    Mant;lon
    Mant;lon

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default

    What do your printlns in the write() method show you about how the lines are being formed before writing?


    out.close(); <<<< THis is inside the loop. The file is closed after every write. I don't know how Java works.
    You should not be able to write to a closed file.
    Try moving this outside of the loop. Do the close ONCE after all writes.

Similar Threads

  1. Having trouble with IO
    By loopsnhoops in forum New To Java
    Replies: 0
    Last Post: 06-02-2011, 04:06 AM
  2. Im having trouble with.......
    By Java Noobie in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 05:06 PM
  3. GUI Trouble
    By rvgsd in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 12:10 AM
  4. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 03:18 PM
  5. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •