似乎无法让扫描程序将JTextField的用户输入与文本文件相匹配。例如:搜索员工编号1121并查看其身份。数字&文本文件中的名称
更新:完整代码,下面有更改
public class custSearch extends JFrame{
private static final long serialVersionUID = 1L;
public JTextField userInput;
// Declaring Window Name, Size & Functions
public custSearch(){
super ("Customer Records");
setSize(630,250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();
//integerFieldFormatter.setGroupingUsed(false);
// Declaring JPanels & New FlowLayouts
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(-2));
// Declaring JLabels
JLabel cs = new JLabel("Search Phone Number:");
// Declaring JButtons
JButton sb1 = new JButton("Search");
// Declaring JFormattedTextFields
//JFormattedTextField cNumber = new JFormattedTextField(integerFieldFormatter);
//cNumber.setColumns(7);
JTextField cNumber = new JTextField();
cNumber.setColumns(7);
// Declaring JPanel Layout
p1.add(cs);
p1.add(cNumber);
p1.add(sb1);
add(p1, BorderLayout.NORTH);
//Create File & Save Details
//sb.addActionListener(new ActionListener() {
//@Override
// public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// File file = new File("Customers.txt");
// try{
// if(file.exists()==false){
// file.createNewFile();
// }
// PrintWriter pw = new PrintWriter(new FileWriter(file, true));
// pw.println(cNumber.getText());
// pw.close();
// }catch(IOException e1){
// JOptionPane.showMessageDialog(null,"<html>Customer File Could Not Be Created<p>Make Sure Program Has File Write Perssions");
// }
//}
//});
sb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try(Scanner sc = new Scanner(new File("Customers.txt"))) {
while(sc.hasNextInt()) {
int cNumber = sc.nextInt();
if(cNumber == Integer.parseInt(userInput.getText()))
JOptionPane.showMessageDialog(null, "Customer Found" + cNumber);
break;
}
}
catch(IOException e1) {
JOptionPane.showMessageDialog(null,"Error");
}
}
});
}
}
获取此错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at custSearch$1.actionPerformed(custSearch.java:83)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
很难找出您提供的代码的问题。你能提供全班和你遇到的错误吗?您似乎也在if语句中使用了赋值运算符:
if(sc = cNumber);
if语句的正确形式是:
if() {
}
或:
if()
only one line allowed;
并且您似乎将Scanner分配给int变量。 cNumber包含你的下一个标记,所以我猜你要做的就是做
之类的事情if(cNumber == 1121) etc..
如果你说有
private JTextField userInput;
您的actionPerformed()必须可见,然后您可以执行以下操作:
if(cNumber == Integer.parseInt(userInput.getText())) {
JOptionPane.showMessageDialog(null, "Customer Found" + cNumber);
}
所以尝试写下这样的东西:
@Override
public void actionPerformed(ActionEvent e) {
try(Scanner sc = new Scanner(new File("Customers.txt"))) {
while(sc.hasNextInt()) {
int cNumber = sc.nextInt();
if(cNumber == Integer.parseInt(userInput.getText()))
JOptionPane.showMessageDialog(null, "Customer Found" + cNumber);
}
}
catch(IOException e1) {
JOptionPane.showMessageDialog(null,"Error");
}
更新尝试创建类似代码:很抱歉,您的代码很难理解,因为我没有其他组件,例如它继承的类,所以我创建的是一个简单的版本你可能需要,对不起,我有点匆忙,所以如果你有任何问题可以随便问我,我无法评论它!基本上它可以满足您的需求,只需确保将Customer.txt放在与src文件夹相同的文件夹中。
所以这是:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField numberTextField;
private JButton submitButton;
public Test() {
setTitle("Get Phone Number");
setLayout(new FlowLayout());
setSize(600, 300);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create TextField, Label and Button
add(new JLabel("Search Phone Number:"));
numberTextField = new JTextField(10);
add(numberTextField);
submitButton = new JButton("Search");
add(submitButton);
//Adding action listener to the button
submitButton.addActionListener(this);
setVisible(true);
pack();
}
public static void main(String[] args) {
new Test();
}
public void findNumber(String path, int number) {
File file = new File(path);
try {
Scanner sc = new Scanner(file);
while(sc.hasNextInt()) {
int i = sc.nextInt();
if(i == number) {
JOptionPane.showMessageDialog(null, "Phone Number: " + number + " is found!");
break;
}
else {
if(!sc.hasNextInt()) {
JOptionPane.showMessageDialog(null, "Sorry phone number: " + number + " is not found!");
break;
}
continue;
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
findNumber("Customers.txt", Integer.parseInt(numberTextField.getText()));
}
}