我正在编写酒店预订程序并使用GUI将来宾信息输入TextFields。我有一个数组,其中这些输入值应该直接(也许这就是我的问题,但不知道如何解决它)。在此之后,我希望Array中的值打印到我创建的文本文件中。继承我的代码:
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
中)我将它们再次放入名为Home
的JFrame类中。之后我将这些变量取为具有文本输入的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);
}
也会喜欢使用除StringTokenizer
以外的任何内容在1行数据之间打印#
。
这是整个错误:
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)
答案 0 :(得分:0)
您多次调用stk.nextToken()
,但该对象只包含一个“#”。这意味着您只能在其上拨打nextToken()
一次。另外Integer.parseInt("#")
可能会引发异常。我建议做这样的事情:
不要在任何内容中添加“#”,而是在toString()
中为Guests
方法添加类似
public String toString() {
return Firstname + "#" + Lastname + "#" + ... + "#" + Email;
}
答案 1 :(得分:0)
// instantiate array after all vars in it have been instantiated
String[] guests = {Firstname, Lastname, Country, IDtype, IDNo,Addr1,Addr2, AreaCode, TelNo, CellNo, Email};
int ct=0;
for(StringTokenizer stk; stk.hasMoreElements() ;){
guests[ct]+=stk.nextToken();
ct+=1;
}
Stringtokenizer是一个“枚举”对象,必须进行迭代,因此需要“布尔测试”“hasMoreElements()”。
答案 2 :(得分:0)
你以错误的方式使用StringTokenizer。 它可能会引发异常,因为没有更多可用的令牌。
您可以使用List而不是大数组。
以下是一些打印到文件中的重构代码 姓名#姓氏#国家#IDtype#IDNO#地址1#地址2#AREACODE#TelNo#CellNo#电子邮件
代码的第一部分:Guests[] GArr = new Guests[1000];
我改为:
List<Guests> guestList = new ArrayList<>();
代码的第三部分:
File GFile = new File("C:\\Users\\Gordy\\Dropbox\\IT\\HotelReservation\\Hotel\\src\\Reservation\\Guests.txt");
try {
if (!GFile.exists()) {
GFile.createNewFile();
}
StringBuilder sb = new StringBuilder;
appendData(sb,Firstname);
appendData(sb,Lastname);
appendData(sb,Country);
appendData(sb,IDtype);
appendData(sb,PassportNo);
appendData(sb,IDNo);
appendData(sb,Addr1);
appendData(sb,Addr2);
appendData(sb,AreaCode);
appendData(sb,TelNo);
appendData(sb,Email,false);
guestList.add(new Guests(Firstname, Lastname, Country, IDtype, IDNo,Addr1,Addr2, AreaCode, TelNo, CellNo, Email));
PrintWriter pw=new PrintWriter(new FileWriter (GFile,true));
pw.println(sb.toString());
pw.close();
CheckIn.hide();
Home.this.setVisible(true);
}catch (Exception e) {
System.out.print("This did not work heres the error: " + e);
}
这是appendData部分
private void appendData(StringBuilder sb,String data){
appendData(sb,data,true);
}
private void appendData(StringBuilder sb,String data,boolean delim){
sb.append(data);
if(delim){
sb.append(delim);
}
}
private void appendDelim(StringBuilder sb){
sb.append("#");
}
如果您只是将其输出到文本文件,我不确定为什么要保留guest虚拟机的数据结构。