我目前正在处理和分配,要求我创建自定义缺少字段异常。我必须从GUI接收数据并确保所有字段都接收输入。我遇到的问题是每当我接受输入并使用.getText()将其放入一个字符串时,我无法在任何类型的if语句中使用它。我的意思是,如果我使用我知道将在JTextField中的确切内容编写if语句,它就不起作用。我对java有些新意,所以我可能只是很容易丢失一些东西。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class PersonFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
static LinkedList<Person> listOfObjects = new LinkedList<Person>();
JTextField fName;
JTextField lName;
JTextField Height;
public PersonFrame()
{
setTitle("Person");
setSize(200, 130);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,2));
add(new JLabel("First Name"));
fName = new JTextField();
add(fName);
add(new JLabel("Last Name"));
lName = new JTextField();
add(lName);
add(new JLabel("Height"));
Height = new JTextField();
add(Height);
JButton jbtSubmit = new JButton("Submit");
JButton jbtCancel = new JButton("Cancel");
add(jbtSubmit);
add(jbtCancel);
SubmitListenerClass listener1 = new SubmitListenerClass();
CancelListenerClass listener2 = new CancelListenerClass();
jbtSubmit.addActionListener(listener1);
jbtCancel.addActionListener(listener2);
setVisible(true);
}
public void CloseWindow()
{
this.setVisible(false);
}
public Person SubmitData()
{
String fn = fName.getText();
String ln = lName.getText();
int h = Integer.parseInt(Height.getText());
Person p = new Person(fn, ln, h);
int i = OneMissingFieldException(p);
int j = MultipleMissingFieldException(p);
if(i == 1 && j == 1)
System.out.println(p);
return p;
}
public void OutputList() throws IOException
{
if(listOfObjects.peekFirst()!=null)
{
PrintWriter pw = new PrintWriter( new FileWriter("outputp.txt") );
Object[] pa = listOfObjects.toArray();
pw.println("Person");
for(int x = 0; x<pa.length; x++)
{
pw.println(pa[x]);
pw.println("");
}
pw.close();
}
}
class CancelListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
CloseWindow();
}
}
class SubmitListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
listOfObjects.add(SubmitData());
CloseWindow();
}
public int OneMissingFieldException(Person p)
{
String fName = ((Person) p).getFName();
try
{
if(fName == "" || fName == null || fName == " ")
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name." + mfe);
return 0;
}
String lName = ((Person) p).getFName();
try
{
if(lName == "" || lName == null || lName == " ")
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a last name." + mfe);
return 0;
}
int height = ((Person) p).getHeight();
try
{
if(!(height >= 0) )
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a height." + mfe);
return 0;
}
return 1;
}
public int MultipleMissingFieldException(Person p)
{
if (p instanceof Person)
{
String fName = ((Person) p).getFName();
String lName = ((Person) p).getFName();
try
{
if((fName == "" || fName == null || fName == " ")&&(lName == "" || lName == null || lName == " "))
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name or last name." + mfe);
return 0;
}
}
String fName = ((Person) p).getFName();
int height = ((Person) p).getHeight();
try
{
if((fName == "" || fName == null || fName == " ")&&(!(height >=0)))
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a first name or height." + mfe);
return 0;
}
String lName = ((Person) p).getFName();
try
{
if((lName == "" || lName == null || lName == " ")&&(!(height >=0)))
throw new MissingFieldException();
}
catch(MissingFieldException mfe){
System.out.println("You did not enter a last name and height." + mfe);
return 0;
}
return 1;
}
}
答案 0 :(得分:2)
使用String,您必须使用string.equals(otherString)
,而不是string == otherString
。
答案 1 :(得分:2)
我认为你犯了一个相当容易的错误,我已经做了很多次。 因为“String”变量不是数据类型(例如int,long和double),而实际上是String类的对象(用大写的第一个字母表示),你必须检查字符串类中的值是否等于要使用.equals(“要检查的字符串值”)函数检查要检查的字符串末尾的值。 举个例子,我想知道myString是否与yourString相同(具有相同的单词/值)。我会写:
myString.equals(yourString); //which would return a boolean
与您将其编写为
的方式相反myString == yourString; //which does not work