我编写了一个酒店预订程序并使用 GUI 将客人信息输入到 TextFields 中。我有一个数组,这些输入值应该直接进入(也许这是我的问题,但不知道如何解决它)。在此之后,我希望将数组中的值打印到我创建的文本文件中。这是我的代码:
Guests[] GArr = new Guests[1000];
public static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
public int cnt = 1;
int count = 0;
String Firstname ;
String Lastname;
String Country ;
String IDtype;
String PassportNo ;
String IDNo ;
String Addr1;
String Addr2 ;
int AreaCode ;
String TelNo ;
String CellNo ;
String Email ;
首先,我将这些变量声明为全局变量(这些变量已经在我的对象类中Guests
)我再次将它们放在我的 JFrame 类中Home
。之后,我让这些变量取了TextFields
有文本输入的值。这是在检查字段是否为空白的方法中:
public Collection<String> getNonBlankFields() {
this.Firstname = NameF.getText();
this.Lastname = NameL.getText();
this.Country = Countr.getText();
this.IDtype = IDTy.getText();
this.PassportNo = PassNo.getText();
this.IDNo = IDNumber.getText();
this.Addr1 = Add1.getText();
this.Addr2 = Add2.getText();
int AreaCode = Integer.parseInt(Area.getText());
this.TelNo = Tel.getText();
this.CellNo = Cell.getText();
this.Email = Em.getText();
this.nonBlankFields = new ArrayList<String>();
this.nonBlankFields.add(this.Firstname);
this.nonBlankFields.add(this.Lastname);
this.nonBlankFields.add(this.Country);
this.nonBlankFields.add(this.IDtype);
this.nonBlankFields.add(this.PassportNo);
this.nonBlankFields.add(this.IDNo);
this.nonBlankFields.add(this.Addr1);
this.nonBlankFields.add(this.Addr2);
this.nonBlankFields.add(this.TelNo);
this.nonBlankFields.add(this.CellNo);
this.nonBlankFields.add(this.Email);
System.out.println(this.nonBlankFields);
return this.nonBlankFields;
}
最后一件事是打印到文件(这是我的主要问题)我尝试了很多方法来解决这个问题,但它超越了这段代码并直接进入了问题:
File GFile = new File("C:\\Users\\Gordy\\Dropbox\\IT\\Hotel Reservation\\Hotel\\src\\Reservation\\Guests.txt");
try {
if (!GFile.exists()) {
GFile.createNewFile();
}
String delim = "#";
String Firstname = NameF.getText();
StringTokenizer stk = new StringTokenizer(delim);
Firstname += stk.nextToken();
System.out.print("FName = " + Firstname);
Lastname += stk.nextToken();
System.out.print("LastName= " + Lastname);
Country += stk.nextToken();
System.out.print("Country= " + Country);
IDtype += stk.nextToken();
IDNo += stk.nextToken();
Addr1 += stk.nextToken();
Addr2 += stk.nextToken();
AreaCode += Integer.parseInt(stk.nextToken());
TelNo += stk.nextToken();
CellNo += stk.nextToken();
Email += stk.nextToken();
GArr[count] = new Guests(Firstname, Lastname, Country, IDtype, IDNo,Addr1,Addr2, AreaCode, TelNo, CellNo, Email);
PrintWriter pw=new PrintWriter(new FileWriter (GFile,true));
pw.println(GArr[count]);
count++;
pw.close();
CheckIn.hide();
Home.this.setVisible(true);
}
catch (Exception e) {
System.out.print("This did not work heres the error: " + e);
}
除了在 1 行的数据之间 StringTokenizer
打印之外,还希望使用任何东西。#
这是整个错误:
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
at Reservation.Home.jButton8ActionPerformed(Home.java:1657)
at Reservation.Home.access$100(Home.java:29)
at Reservation.Home$2.actionPerformed(Home.java:269)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.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)