Results 1 to 4 of 4

Thread: Java I/O Help!

  1. #1
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default Java I/O Help!

    Hello
    I created an application where a user registers by filling some fields. All I want is to make the "Submit" button to save the written data to a txt file but for some reason I can't do it! Somewhere here there is an error! Here is the code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    
    import java.awt.Rectangle;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.io.BufferedWriter;
    import java.io.File;
    
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    
    import java.io.PrintWriter;
    
    import java.util.Scanner;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    public class Form extends JFrame {
    
    String[] relationship = {"", "Single", "In a Relationship", "Engaged", "Married", "Divorced", "In an Open Relationship", "Widowed"};
    String[] countries = {"", "Greece", "America", "England", "Italy", "Germany", "Russia", "Spain", "Australia"};
    
    private BorderLayout layoutMain = new BorderLayout();
    private JPanel panelCenter = new JPanel();
    private JLabel statusBar = new JLabel();
    private JLabel firstnamel = new JLabel();
    private JLabel lastnamel = new JLabel();
    private JLabel marstatl = new JLabel();
    private JLabel countryl = new JLabel();
    private JLabel statel = new JLabel();
    private JLabel sexl = new JLabel();
    private JButton newB = new JButton();
    private JButton openB = new JButton();
    private JButton submitB = new JButton();
    private JTextField firstnameTF = new JTextField();
    private JTextField lastnameTF = new JTextField();
    private JTextField stateTF = new JTextField();
    private JRadioButton maleRB = new JRadioButton();
    private JRadioButton femaleRB = new JRadioButton();
    private JComboBox relationshipsCB = new JComboBox(relationship);
    private JComboBox countriesCB = new JComboBox(countries);
    private JButton infoB = new JButton();
    private JLabel warningJL = new JLabel();
    
    
    public Form() {
    try {
    jbInit();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    private void jbInit() throws Exception {
    this.getContentPane().setLayout( layoutMain );
    panelCenter.setLayout( null );
    this.setSize(new Dimension(496, 585));
    statusBar.setText( "" );
    firstnamel.setText("First Name");
    firstnamel.setBounds(new Rectangle(35, 160, 70, 25));
    lastnamel.setText("Last Name");
    lastnamel.setBounds(new Rectangle(35, 200, 75, 25));
    marstatl.setText("Marrital Status");
    marstatl.setBounds(new Rectangle(35, 305, 90, 20));
    countryl.setText("Country");
    countryl.setBounds(new Rectangle(35, 360, 105, 20));
    statel.setText("State");
    statel.setBounds(new Rectangle(35, 420, 45, 25));
    sexl.setText("***");
    sexl.setBounds(new Rectangle(35, 260, 50, 20));
    newB.setText("New Profile");
    newB.setBounds(new Rectangle(20, 70, 170, 40));
    newB.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    newB_actionPerformed(e);
    }
    });
    openB.setText("Open Existing Profile");
    openB.setBounds(new Rectangle(255, 70, 170, 40));
    submitB.setText("Submit Profile");
    submitB.setBounds(new Rectangle(20, 485, 160, 60));
    submitB.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    submitB_actionPerformed(e);
    }
    });
    firstnameTF.setBounds(new Rectangle(125, 155, 235, 20));
    lastnameTF.setBounds(new Rectangle(125, 205, 235, 20));
    maleRB.setText("Male");
    maleRB.setBounds(new Rectangle(130, 265, 86, 18));
    femaleRB.setText("Female");
    femaleRB.setBounds(new Rectangle(245, 260, 85, 25));
    relationshipsCB.setBounds(new Rectangle(130, 305, 180, 20));
    countriesCB.setBounds(new Rectangle(130, 360, 180, 20));
    infoB.setText("See your Slected Info");
    infoB.setBounds(new Rectangle(295, 485, 160, 60));
    infoB.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    infoB_actionPerformed(e);
    }
    });
    warningJL.setText("ALL FIELDS ARE NECESSARY TO BE FILLED!");
    warningJL.setBounds(new Rectangle(115, 15, 225, 35));
    warningJL.setForeground(new Color(231, 0, 0));
    stateTF.setBounds(new Rectangle(130, 420, 220, 20));
    this.getContentPane().add(statusBar, BorderLayout.SOUTH);
    this.getContentPane().add(panelCenter, BorderLayout.CENTER);
    panelCenter.add(warningJL, null);
    panelCenter.add(infoB, null);
    panelCenter.add(stateTF, null);
    panelCenter.add(countriesCB, null);
    panelCenter.add(relationshipsCB, null);
    panelCenter.add(femaleRB, null);
    panelCenter.add(maleRB, null);
    panelCenter.add(sexl, null);
    panelCenter.add(firstnameTF, null);
    panelCenter.add(lastnameTF, null);
    panelCenter.add(submitB, null);
    panelCenter.add(openB, null);
    panelCenter.add(newB, null);
    panelCenter.add(statel, null);
    panelCenter.add(countryl, null);
    panelCenter.add(marstatl, null);
    panelCenter.add(lastnamel, null);
    panelCenter.add(firstnamel, null);
    
    }
    
    private void infoB_actionPerformed(ActionEvent e) {
    String a = firstnameTF.getText();
    String b = lastnameTF.getText();
    String c = stateTF.getText();
    int d = relationshipsCB.getSelectedIndex();
    int g = countriesCB.getSelectedIndex();
    
    if (!maleRB.isSelected() && !femaleRB.isSelected() || a.isEmpty() || b.isEmpty() || c.isEmpty() || d == 0 || g == 0)
    JOptionPane.showMessageDialog(null,"Please Complete all the fields! ");
    
    else
    
    JOptionPane.showMessageDialog(null, "YOUR INFO: \n" + "First Name: " + firstnameTF.getText() + "\n" + "Last Name: " + lastnameTF.getText() + "\n" + "***: " + maleRB.getText() +
    "\n" + "Marrital Status: " + relationshipsCB.getSelectedItem() + "\n" + "Country: " + countriesCB.getSelectedItem() + "\n" + "State: " + stateTF.getText());
    
    }
    
    private void newB_actionPerformed(ActionEvent e) {
    
    firstnameTF.setText("");
    lastnameTF.setText("");
    stateTF.setText("");
    maleRB.setSelected(false);
    femaleRB.setSelected(false);
    relationshipsCB.setSelectedIndex(0);
    countriesCB.setSelectedIndex(0);
    }
    
    
    class FileSave {
    public void storing() throws FileNotFoundException, Exception {
    
    Scanner input = new Scanner(firstnameTF.getText());
    PrintWriter output = new PrintWriter ("c:
    data2.txt");
    
    String firstname = input.firstnameTF.getText();
    String lastname = lastnameTF.getText();
    String sexm = maleRB.getText();
    String sexf = femaleRB.getText();
    int relationship = relationshipsCB.getSelectedIndex();
    int countries = countriesCB.getSelectedIndex();
    
    output.println(firstname);
    output.write(firstname);
    input.close();
    output.close();
    
    }
    }
    
    private void submitB_actionPerformed(ActionEvent e){
    
    class store {
    public void Storage() throws FileNotFoundException, Exception {
    
    FileSave obj = new FileSave();
    obj.storing();
    
    }
    }
    }
    
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Too much code to look at.

    Start with something simple. Create a Jframe with a button. When you click the button, write some text to a file.

    Then once you get that working add a text field. Now when you click the button, get the text from the text field and write it to the file.

    Now that you understand the basics you apply the solution to your real program.

    If you have problems with the first couple of steps then post your Short, Self Contained, Correct Example that demonstrates the problem.
    Last edited by camickr; 05-29-2011 at 11:07 PM.

  3. #3
    louboulos is offline Member
    Join Date
    Mar 2011
    Posts
    25
    Rep Power
    0

    Default

    This is the class with the method that writes to the file and below is the button.

    Java Code:
    class FileSave {
    public void storing() throws FileNotFoundException, Exception {
    
    Scanner input = new Scanner(firstnameTF.getText());
    PrintWriter output = new PrintWriter ("c:
    data2.txt");
    
    String firstname = input.firstnameTF.getText();
    String lastname = lastnameTF.getText();
    String sexm = maleRB.getText();
    String sexf = femaleRB.getText();
    int relationship = relationshipsCB.getSelectedIndex();
    int countries = countriesCB.getSelectedIndex();
    
    output.println(firstname);
    output.write(firstname);
    input.close();
    output.close();
    
    }
    }
    
    private void submitB_actionPerformed(ActionEvent e){
    
    class store {
    public void Storage() throws FileNotFoundException, Exception {
    
    FileSave obj = new FileSave();
    obj.storing();
    
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please have another look at the link that camickr provided on creating an SSCCE as it could make helping you much easier. Also please post only formatted code. The easier you make it for folks to help you, the better your chances of getting decent help. Much luck!

Posting Permissions

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