gpt4 book ai didi

java - Java-无效的方法声明;方法的返回类型

转载 作者:行者123 更新时间:2023-12-02 11:12:49 24 4
gpt4 key购买 nike

我的问题是在两个不同的程序中进行作业。一种任务是将文档写入文件,第二种任务是从文件读取。在这两种情况下,我都会收到相同的错误。我不确定自己在做什么错。请原谅我的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();
}
}

对于过多的注释,我也深表歉意,因为我更改了代码,其中的一些注释根本没有意义。如果需要,我愿意重新发布代码。我通常不寻求作业帮助,但是由于这对我的成绩至关重要,而且我觉得自己快要完成作业了,尽管我可以寻求一些指导。谢谢。

最佳答案

您有一个类名WriteContacts,但是您正在尝试使用无效的名称public WriteContacts2()创建一个构造函数。构造函数名称应与类名称完全匹配。

可以将您的类名称更改为WriteContacts2,也可以将无效的构造函数从以下位置更改:

public WriteContacts2() { // }


public WriteContacts() { // }

同样适用于 ReadContacts类。

关于java - Java-无效的方法声明;方法的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com