Search code examples
javaarraystext-filesjtextfieldprintwriter

Cant understand why this doesn't print to the text file


Im coding a hotel reservation program and im using a GUI to input the Guests info into TextFields. I have an array where these input values should go straight to (maybe thats my problem, but donno how to fix it). After this I want the values in the Array to print to the text file I had created. Heres my code:

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 ;

First I have declared these variables as global (these variables are already in my object class Guests) i put them again in my JFrame class called Home. After that I have made these variables take the value of the TextFields that have text input into them. This is within a method that checks to see if the fields are blank:

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;
}

Last thing is the printing to the file (this is my major problem) ive tried so many things to fix this but it surpasses this code and goes straight to the catch:

     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);
        }

would also love to use anything but StringTokenizer to print with # in between the data on 1 line.

this is the whole error:

    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)

Solution

  • You are calling stk.nextToken() multiple times, but that object only holds a single "#". This means that you can only call nextToken() on it once. Also Integer.parseInt("#") probably throws an exception. I would suggest doing something like this :

    Don't add "#" to anything and instead for your toString() method in Guests do something like

    public String toString() {
        return Firstname + "#" + Lastname + "#" + ... + "#" + Email;
    }