Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Reading and Writing to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Reading and Writing to a file" Watch "Reading and Writing to a file" New topic
Author

Reading and Writing to a file

Cheryl Irwin
Greenhorn

Joined: Oct 20, 2012
Posts: 8
Hi all! I am desperate for some help sorting out the problems I am having with this week's assignment in my Java class. I am severely struggling with GUI displays, exception handling, and reading/writing files, all of which are in this assignment!

I have to create a file that allows entry of a name, age, email, and phone number into a file in a file that can then be retrieved. The age has to be validated to be between 0 and 120 and I need exception handling in case the file cannot be opened. Here is my current code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;

public class ContactFrame extends JFrame
{
//Set Program Variables
private JPanel NamePanel;
private JLabel Name;
private JTextField name;
private JPanel AgePanel;
private JLabel Age;
private JTextField age;
private JPanel PhonePanel;
private JLabel Phone;
private JTextField phone;
private JPanel EmailPanel;
private JLabel Email;
private JTextField email;
private JPanel ButtonPanel;
private JButton Save;
private JButton Exit;
private JButton View;

public ContactFrame()
{
super ("Contacts");

//Create Name Panel
Name = new JLabel("Name : ");
name = new JTextField(20);

NamePanel.add(Name);
NamePanel.add(name);

//Create Age Panel
Age = new JLabel("Age : ");
age = new JTextField(3);

AgePanel.add(Age);
AgePanel.add(age);

//Create Phone Panel
Phone = new JLabel("Phone Number : ");
phone = new JTextField(10);

PhonePanel.add(Phone);
PhonePanel.add(phone);

//Create Email Panel
Email = new JLabel("Email : ");
email = new JTextField(20);

EmailPanel.add(Email);
EmailPanel.add(email);

//Create Button Panel
Save = new JButton("Save");
Exit = new JButton("Exit");

Save.addActionListener(new SaveListener());
Exit.addActionListener(new ExitListener());
View.addActionListener(new ViewListener());

ButtonPanel.add(Save);
ButtonPanel.add(Exit);

//Create Border Layout and add to the window
setLayout (new BorderLayout());

add(NamePanel, BorderLayout.NORTH);
add(AgePanel, BorderLayout.WEST);
add(PhonePanel, BorderLayout.CENTER);
add(EmailPanel, BorderLayout.EAST);
add(ButtonPanel, BorderLayout.SOUTH);

//Pack and display the window
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350,150);
setVisible(true);
}
private class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Close the program when exit button clicked
System.exit(0);
}
}
private class ViewListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
File file = new File(ContactList.txt);
Scanner inputFile = new Scanner(file);

while(inputFile.hasNext())
{
String name = inputFile.nextLine();
String age = inputFile.next();
String phone = inputFile.next();
System.out.println(email);
}
inputFile.close();
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name,
email;
int age;
double phone;

if (age < 0)
{
JOptionPane.showMessageDialog(null, "Please enter a valid age "
+ "0 - 120");
}
else if (age > 120)
{
JOptionPane.showMessageDialog(null, "Please enter a valid age "
+ "0 - 120");
}
else
{
//Save the data when save button is clicked
FileWriter fileWriter = new FileWriter("ContactList.txt", true);
PrintWriter outputFile = new PrintWriter(fileWriter);
outputFile.println(name);
outputFile.println(age);
outputFile.println(phone);
outputFile.println(email);
outputFile.close();
}
}
}
public static void main(String[] args)
{
//Run the program
ContactFrame CF = new ContactFrame();
}
}
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
Please use code tags when posting code and it generally helps if you ask a question. What would you like help with?
Cheryl Irwin
Greenhorn

Joined: Oct 20, 2012
Posts: 8
Sorry this is my first time posting here. I need some help figuring out what is wrong with my coding. I am specifically having the most trouble with file creation and retrieval and exception handling. I have not been able to figure out why my coding is not working at all. Can you post an example of creating and retrieving a file from a GUI and give me some tips about exception handling? I have seen examples using a try code line, is that what I am missing? If so, what is the proper format of that coding? Do I need separate classes rather than nested classes to properly code this file? Any tips can help me since I am so lost I can't even figure out what to ask. I am reposting my code with the code tags.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;

public class ContactFrame extends JFrame
{
//Set Program Variables
private JPanel NamePanel;
private JLabel Name;
private JTextField name;
private JPanel AgePanel;
private JLabel Age;
private JTextField age;
private JPanel PhonePanel;
private JLabel Phone;
private JTextField phone;
private JPanel EmailPanel;
private JLabel Email;
private JTextField email;
private JPanel ButtonPanel;
private JButton Save;
private JButton Exit;
private JButton View;

public ContactFrame()
{
super ("Contacts");

//Create Name Panel
Name = new JLabel("Name : ");
name = new JTextField(20);

NamePanel.add(Name);
NamePanel.add(name);

//Create Age Panel
Age = new JLabel("Age : ");
age = new JTextField(3);

AgePanel.add(Age);
AgePanel.add(age);

//Create Phone Panel
Phone = new JLabel("Phone Number : ");
phone = new JTextField(10);

PhonePanel.add(Phone);
PhonePanel.add(phone);

//Create Email Panel
Email = new JLabel("Email : ");
email = new JTextField(20);

EmailPanel.add(Email);
EmailPanel.add(email);

//Create Button Panel
Save = new JButton("Save");
Exit = new JButton("Exit");

Save.addActionListener(new SaveListener());
Exit.addActionListener(new ExitListener());
View.addActionListener(new ViewListener());

ButtonPanel.add(Save);
ButtonPanel.add(Exit);

//Create Border Layout and add to the window
setLayout (new BorderLayout());

add(NamePanel, BorderLayout.NORTH);
add(AgePanel, BorderLayout.WEST);
add(PhonePanel, BorderLayout.CENTER);
add(EmailPanel, BorderLayout.EAST);
add(ButtonPanel, BorderLayout.SOUTH);

//Pack and display the window
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350,150);
setVisible(true);
}
private class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Close the program when exit button clicked
System.exit(0);
}
}
private class ViewListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
File file = new File(ContactList.txt);
Scanner inputFile = new Scanner(file);

while(inputFile.hasNext())
{
String name = inputFile.nextLine();
String age = inputFile.next();
String phone = inputFile.next();
System.out.println(email);
}
inputFile.close();
}
}
private class SaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name,
email;
int age;
double phone;

if (age < 0)
{
JOptionPane.showMessageDialog(null, "Please enter a valid age "
+ "0 - 120");
}
else if (age > 120)
{
JOptionPane.showMessageDialog(null, "Please enter a valid age "
+ "0 - 120");
}
else
{
//Save the data when save button is clicked
FileWriter fileWriter = new FileWriter("ContactList.txt", true);
PrintWriter outputFile = new PrintWriter(fileWriter);
outputFile.println(name);
outputFile.println(age);
outputFile.println(phone);
outputFile.println(email);
outputFile.close();
}
}
}
public static void main(String[] args)
{
//Run the program
ContactFrame CF = new ContactFrame();
}
}
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

As Tony said, and I will just refer you to the right location: UseCodeTags (<-click and read the directions). Also for future reference you should TellTheDetails and IsolateTheProblem you're facing so you can get help faster than posting entire code and saying that you just have a problem with it.

As for the code you posted, it can't even be compiled.
First of all, this is not a valid statement:

though this might be:


Then the block(s) of statements that you have in your SaveListener really don't make sense to me:

You just declared variable age and immediately you are checking if it's less then 0, even though age is not initialized. Even if you initialize it here (say to 0) you won't get the desired result, since it will always save that value to the file. You need to pick the value from your gui components and then do the checking.

Also, this line:

throws an exception so it should be inside try-catch block, where you would handle it.

Anyway, lots of syntax errors you should first check and then you're welcome to ask any specific question you might have about your problem.

Edit: Greetings to all the Ranchers, after a short break!


The quieter you are, the more you are able to hear.
Cheryl Irwin
Greenhorn

Joined: Oct 20, 2012
Posts: 8
OK it is a place to start. Thanks!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

For your reference I would suggest you check this tutorial, especially the Line-Oriented I/O section, because that seems like something you are looking for.
Also you might want to check tutorial on Exceptions.

Can you post an example of creating and retrieving a file from a GUI and give me some tips about exception handling?

Once you catch up with I/O from command line, doing it from GUI is similar; the difference is just in that you will probably invoke those actions as a result of some kind of event that you have listener for.

Cheryl Irwin
Greenhorn

Joined: Oct 20, 2012
Posts: 8
I appreciate the tutorial links they are helping me tremendously and are exactly what I was looking for. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading and Writing to a file
 
Similar Threads
Illegal argument exception: cannot add parent to itself
BoxLayout not working as advertised?
writing to text files
subclasses throw null exception when using objectoutput stream
Splash Window