我刚开始学习java,这几天试着写一个宿舍管理系统,刚开始写就遇到问题写不下去了,希望大家可以帮助解决下。出现第一个界面的时候还没问题,但是当运行到第二个界面的时候就跳出这个问题了
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For inpu
t string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Long.parseLong(Long.java:424)
at java.lang.Long.parseLong(Long.java:461)
at Graph2.<init>(Graph2.java:13)
at Graph1$1$1.actionPerformed(Graph1.java:33)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For inpu
t string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.
java:48)
at java.lang.Long.parseLong(Long.java:424)
at java.lang.Long.parseLong(Long.java:461)
at Graph2.<init>(Graph2.java:13)
at Graph1$1$1.actionPerformed(Graph1.java:33)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:19
95)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav
a:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
String name;
String mima;
Long id;
Student(){
name=null;
mima=null;
id=0L;
}
//Student类的构造函数
Student(String _name,String _mima,Long _id){
name=_name;
mima=_mima;
id=_id;
}
//返回姓名
public String getName(){
return name;
}
//返回学号
public Long getId(){
return id;
}
//允许修改密码
public void setMima(String m){
mima=m;
}
//返回密码
public String getMima(){
return mima;
}
//显示学生的信息
@Override public String toString(){
return "姓名:"+name+" \n学号:"+id;
}
}
import java.awt.*;
import java.awt.event.*;
public class StudentOn extends JFrame{
int i;
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
JButton btn3=new JButton("重置");
JTextField testField1=new JTextField("",20);
JTextField testField2=new JTextField(20);
StudentRun studentRun=new StudentRun();
Student stu=studentRun.findById(Long.parseLong(testField1.getText()));
public StudentOn() {}
public StudentOn(int _i){
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("学号:"));
c.add(testField1);
c.add(new JLabel("密码:"));
c.add(testField2);
JPanel panel=new JPanel();
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
c.add(panel,"South");
setTitle("学生宿舍信息管理系统");
setSize(300,150);
setVisible(true);
setLocation(450,300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boolean bool=true;
if(stu==null) bool=false;
if(bool){
if((stu.mima).equals(testField2.getText())){
btn1.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
new SudentInformation();
}
});
}
else{
btn1.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"您的密码错误!");
System.exit(0);
}
});
}
}
else{
btn1.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"没有该学生的信息!");
}
});
}
btn2.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
btn3.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
testField1.setText("");
testField2.setText("");
}
});
}
Student returnFound(){
return stu;
}
}
一般出现异常可以仔细看一下是什么原因,因为这些英语的理解还是比较简单的。
“数字格式异常”“由于输入字符串”
下面是第一个界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class WelcomeGraph extends JFrame{
JButton btn1=new JButton("确定");
JButton btn2=new JButton("取消");
CheckboxGroup cbg=new CheckboxGroup();
Checkbox checkbox1=new Checkbox("学生",cbg,false);
Checkbox checkbox2=new Checkbox("管理员",cbg,false);
public WelcomeGraph(){
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(new JLabel("进入系统的身份:"));
c.add(checkbox1);
c.add(checkbox2);
JPanel panel=new JPanel();
panel.add(btn1);
panel.add(btn2);
c.add(panel,"South");
setTitle("学生宿舍信息管理系统");
setSize(300,120);
setVisible(true);
setLocation(450,300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
checkbox1.addItemListener(new ItemListener(){
@Override public void itemStateChanged(ItemEvent ev){
btn1.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
new StudentOn(1);
}
});
btn2.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
});
checkbox2.addItemListener(new ItemListener(){
@Override public void itemStateChanged(ItemEvent ev){
btn1.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
new StaffOn();
}
});
btn2.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
});
}catch(Exception ex){
System.exit(0);
}
}
}
import java.util.*;
import java.io.*;public class StudentRun{
public static int MAXSIZE=4;
public static Student[] students=new Student[MAXSIZE];
public static int count=0; public static void save()throws Exception{
for(Student temp:students){
ObjectOutputStream obj=new ObjectOutputStream(new FileOutputStream("Student.txt",true));
obj.writeObject(temp);
obj.close();
}
} public static void load()throws Exception{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("Student.txt"));
if(in==null){
System.out.println("没有信息");
}
else{
for(int i=0;i<=count;i++){
Object obj=in.readObject();
Student[] stu=new Student[MAXSIZE];
stu[i]=(Student)obj;
System.out.print(stu[i].toString());
}
}
in.close();
} public static void add(Student stu){
if(count==MAXSIZE){
System.out.println("renman");
}
else{
count++;
students[count]=stu;
}
}
public static void dispAll(){
for(Student temp:students){
System.out.println(temp.toString());
}
} /*public static void findById(Double id){
int i=0;
for(Student temp:students){
if(temp.id==id){
System.out.println(temp.toString());
break;
}
i++;
}
if(i>=count){
System.out.println("没有找到!");
}
}*/ /*public static boolean findById(Long id){
int i=0;
for(Student temp:students){
if(temp.id==id){
return true;
}
}
return false;
}*/ public static Student findById(Long id){
for(Student temp:students){
if(temp.id==id){
return temp;
}
}
return null;
} public static void findByName(String name){
int i=0;
for(Student temp:students){
if(temp.name.equals(name)){
System.out.println(temp.toString());
break;
}
i++;
}
if(i>=count){
System.out.println("没有找到!");
}
}
public static void DelById(Long id){
int i;
for(i=0;i<MAXSIZE;i++){
if(students[i].id==id){
students[i]=null;
break;
}
}
if(i>=count){
System.out.println("没有找到!");
}
} //private static Scanner scanner=new Scanner(System.in);
public static void main(String[] args)throws Exception{
new Student("lizhuo","123",1L);
new Student("zhumin","123",2L);
count=2;
new WelcomeGraph();
}
}
try{
stuid=Long.parseLong(testField1.getText());
}catch(Exception e){}
Student stu=studentRun.findById(stuid);
不明白楼主学Java为什么对awt swing 感兴趣,这技术在公司都不用的
可以这样改Student stu= null;
if(!"".equals(testField1.getText()))//不为空再转换为空可以提示或者不做操作 最好先用正则表达式检测下 再进行转换
stu =studentRun.findById(Long.parseLong(testField1.getText()));
t string: ""空字符串是不能转换成数值型的,要添加判断过滤或加异常处理。
Student stu=studentRun.findById(Long.parseLong(testField1.getText()));
学号做了转换(Long.parseLong)但是学号为“” 导致转换异常。
判断一下,学号为空,或者不符合要求 提示就行了。
Student stu= null;
if(!"".equals(testField1.getText()))
{
stu =studentRun.findById(Long.parseLong(testField1.getText()));
}else
{
JOptionPane.showMessageDialog(null,"请输入学号!");
}