我的问题在于两个不同的作业程序。一个分配是将文档写入文件,第二个分配部分是从文件中读取。在这两种情况下我都得到同样的错误。我不完全确定我做错了什么。请原谅我的Nooby性质。
这是编写代码
//WriteContactsAssignment: A program to enter contact information to be written to a Contacts file
//Author: Daniel Golightly
//Date: February 26, 2014
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.*;
import java.awt.Dialog;
import java.awt.event.*;
import java.io.*;
import java.io.FileWriter;
import java.lang.System.*;
public class WriteContacts extends JFrame implements ActionListener
{
//GUI COMPONENTS
JLabel nameLabel;
JLabel ageLabel;
JLabel emailLabel;
JLabel phoneLabel;
JTextField nameField;
JTextField ageField;
JTextField emailField;
JTextField phoneField;
JButton btnWrite;
JPanel panelWrite;
//VARIABLES FOR WRITING
String name;
String email;
String phone;
String ageString; //holds age to be converted to integer for verifying
int age; //variable for age after conversion to integer for verifying
//CONSTRUCTOR FOR FILE WRITING
public static void writeToFile(String textToWrite)
{
final String outputPath = "ContactsFile.txt";
try
{
FileWriter contactWriter = new FileWriter(outputPath);
contactWriter.write(textToWrite);
contactWriter.close();
}
catch (IOException ex2)
{
JOptionPane.showMessageDialog(frame,
"The file did not create properly",
"Oops!", JOptionPane.ERROR_MESSAGE);
}
}
//GUI CONSTRUCTOR
public WriteContacts2()
{
setUndecorated(false);//GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI
setResizable(false);
setSize(400, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Contact Writer");
GridLayout fieldsLayout = new GridLayout(5, 2, 10, 10);
panelWrite = new JPanel();
panelWrite.setLayout(fieldsLayout);
nameLabel = new JLabel("Name: ");
panelWrite.add(nameLabel);
nameField = new JTextField(30);
panelWrite.add(nameField);
ageLabel = new JLabel("Age: ");
panelWrite.add(ageLabel);
ageField = new JTextField(2);
panelWrite.add(ageField);
phoneLabel = new JLabel("Phone Number: ");
panelWrite.add(phoneLabel);
phoneField = new JTextField(10);
panelWrite.add(phoneField);
emailLabel = new JLabel("Email: ");
panelWrite.add(emailLabel);
emailField = new JTextField(50);
panelWrite.add(emailField);
panelWrite.add(new JLabel(" "));
btnWrite = new JButton("Submit");
panelWrite.add(btnWrite);
add(panelWrite);
setVisible(true);
}
//VALIDATES ENTRY, WRITES TO FILE, CLEARS FIELDS FOR ADDITIONAL ENTRY
public void actionPerformed(ActionEvent e)
{
name = nameField.getText();
email = emailField.getText();
phone = phoneField.getText();
ageString = ageField.getText();
try
{
age = Integer.parseInt(ageString);
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(frame,
"The Age you entered is not a number",
"Age Error",
JOptionPane.ERROR_MESSAGE);
}
if (0 > age || age > 120)
{
JOptionPane.ShowMessageDialog(frame,
"The age you entered is invalid. Please enter between 1 and 120",
"Age Error",
JOptionPane.ERROR_MESSAGE);
}
else if (ContactsFile.exists())
{
writeToFile("Name: " + name + "/n");
writeToFile("E-Mail: " + email + "/n");
writeToFile("Age: " + age + "/n");
writeToFile("Phone Number: " + phone + "/n" + "/n");
//OPEN A FILE WRITER HERE TO WRITE TO THE NEXT LINE OF THE FILE, WRITING EACH VARIABLE TO A DIFFERENT LINE
//LEAVE TWO BLANK LINES AT THE BOTTOM OF THE FILE
//CLOSE THE WRITER
}
nameField.setText("");
emailField.setText("");
ageField.setText("");
phoneField.setText("");
//WRITE MESSAGE THAT WRITE WAS SUCCESSFUL
//CLEAR ENTRY FIELDS
//EXIT EVENT
}
//MAIN ARGS
public static void main (String[] args)
{
WriteContacts2 WriteGUI = new WriteContacts2();
}
}
这是阅读代码
//ReadContactsAssignment: A program to read contact information from a Contacts file
//Author: Daniel Golightly
//Date: February 26, 2014
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.System.*;
public class ReadContacts extends JFrame implements ActionListener
{
//GUI COMPONENTS
JLabel contactInfo;
JTextArea displayArea;
JButton btnRead;
JPanel panelRead;
//GUI CONSTRUCTOR
public ReadContacts2 ()
{
setUndecorated(false); //GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI GUI
setSize(450, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Contact List");
GridLayout readLayout = new GridLayout(2, 1);
panelRead = new JPanel();
panelRead.setLayout(readLayout);
displayArea = new JTextArea();
panelRead.add(displayArea);
btnRead = new JButton("Read Contacts");
panelRead.add(btnRead);
add(panelRead);
setVisible(true);
}
//READS THE CONTACTS.TXT FILE TO THE TEXTAREA
public void actionPerformed(ActionEvent e)
{
File ContactsFile = File("Week3Contacts.txt");
ContactsFile.createNewFile("Week3Contacts.txt", false).close();
try //OR SOMETHING VERY SIMILAR
{
if(ContactsFile.exists())
{
FileReader reader = new FileReader( "Week3Contacts.txt" );
BufferedReader br = new BufferedReader(reader);
edit.read( br, null );
br.close();
edit.requestFocus();
}
}
catch (FileNotFoundException ex)
{
JOptionPane.showMessageDialog(frame,
"The file could not be found or is corrupted. Contact your IT administrator.",
"Oops", JOptionPane.ERROR_MESSAGE);
}
//VALIDATE THAT THE FILE EXISTS
//IF FILE VALIDATION FAILS, DISPLAY FILE NOT FOUND, CONTACT ADMIN MESSAGE
//OTHERWISE CONTINUE EVENT
//OPEN READER AND READ TO THE TEXTAREA
//CLOSE THE READER TO KEEP IT FROM EATING RESOURCES AND ALLOW THE FILE TO BE INTERACTED WITH
//EXIT EVENT
}
//MAIN ARGS
public static void main (String[] args)
{
ReadContacts2 ReadGUI = new ReadContacts2();
}
}
我也为多余的评论道歉,其中一些根本没有意义,因为我已经改变了我的代码。如果需要,我愿意重新发布代码。我一般不会在作业上寻求帮助,但由于这对我的成绩至关重要,而且我觉得自己已经完成了这项任务,我觉得可以提出一些指导。感谢。
答案 0 :(得分:1)
您有一个类名WriteContacts
,但您正在尝试创建一个名为public WriteContacts2()
且无效的构造函数。构造函数名称应与类名完全匹配。
将您的班级名称更改为WriteContacts2
或更改无效的构造函数:
public WriteContacts2() { // }
到
public WriteContacts() { // }
同样适用于您的ReadContacts
课程。