guys please help me with this. my problem is that on PaymentNewGUI class i am finding it hard to extract the entrance time from the file to be displayed on the gui if correct ticket number is added, reference to enterListener method in paymentnewGUI class thank you

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Date;
import java.lang.*;

public class PaymentNewGUI extends JFrame{
	
	private JButton tenP, twentyP, fiftyP, onePound, twoPound;
	JTextField ticketID;
	
	double amountDue = 0.00;
	double amountInserted;
	Label lEntranceTime;
	Label lPaymentTime;
	Label lDue;
	Label lInserted;
	Label lMessage = new Label();
	Container content;
	JPanel ticketPanel;
	
	/***Key Variable***/
	long entranceTime;
	long paymentTime;
	String ticketStatus;
	double balance;
	
	
		
	
	public PaymentNewGUI() {
		
		lEntranceTime = new Label("Entrance Time: " );
		lPaymentTime = new Label("Payment Time: " );
		lDue = new Label("Amount Due:   \u00A3" + amountDue);
		lInserted = new Label("Amount Instered:   \u00A3" + amountInserted);
		
		
		content = getContentPane();
		content.setLayout(new BorderLayout());
		this.setTitle("Enter Ticket Information");
	 	
		Label lTicketID = new Label("Enter Ticket No:");
		JButton enterButton = new JButton("Enter");
		enterButton.addActionListener(new enterListener());
		ticketID = new JTextField(9);
		ticketPanel = new JPanel();
    	ticketPanel.setLayout(new GridLayout(2,0));
		ticketPanel.add(lTicketID);
		ticketPanel.add(ticketID);
		ticketPanel.add(enterButton);
		ticketPanel.setVisible(true);
		content.add(ticketPanel, BorderLayout.NORTH);
		
		/*********************Ticket Number Listener************************************/
		
		
		/*********************Label Addition to Frame Content***********************/
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(4,0));
		labelPanel.setVisible(true);
		labelPanel.add(lEntranceTime);
		labelPanel.add(lPaymentTime);
		labelPanel.add(lDue);
		labelPanel.add(lInserted);
		content.add(labelPanel, BorderLayout.WEST);
		/*********************Label Only*************************/
		
		
	}//end PaymentNewGUI constructor
	
	
	public class enterListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			
			Ticket ticket = new Ticket();
			int ticketIDNumber = Integer.parseInt(ticketID.getText());
			String loadMessage = ticket.loadTicket(ticketIDNumber);
			lMessage.setText(loadMessage);
			ticketPanel.add(lMessage, BorderLayout.SOUTH);
			
			entranceTime = Ticket.entranceTime;
			paymentTime = Ticket.paymentTime;
			ticketStatus = Ticket.ticketStatus;
		    balance = Ticket.balance;
			
			if (loadMessage.equals("The Ticket has loaded successfully.")){
				
				
				System.out.println("" + Ticket.entranceTime);
			//	 lEntranceTime.setText("Entrance Time: " + entranceTime);
				
	    	} else  {
	    				System.out.println("Incorrect Ticket Information Given!");
		}

	
		}}

	
	public static void main(String args[]) {
		PaymentNewGUI frame = new PaymentNewGUI();
		frame.pack();
		frame.setVisible(true);
		frame.setSize(400,400);
		}
	
	
	
	
}
import java.util.Date;
import java.lang.*;
import java.io.*;
import java.util.Calendar;

public class Ticket {
	public TicketInfo x;
	public int tNumber;
	public static long entranceTime;
	public static long paymentTime;
	public static String ticketStatus;
	public static double balance;
	/********************************************************************************************/
	public String createTicket(){
		
	tNumber = (int)(Math.random() * 999999)+100000;


	
		File dir = new File("Ticket");
		boolean exists = dir.exists();
		if (!exists){
			dir.mkdir();
		} // end if

		TicketInfo x = new TicketInfo(entranceTime, paymentTime, ticketStatus, balance);
		 
   

		String fileName = "Ticket/"+tNumber+".dat";
		File ticket = new File(fileName);
		
		if(ticket.exists()){
			//generateTicketNumber();
			return "Try again";
		}
		else {
			try{
				FileWriter fileWriter = new FileWriter(fileName);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(x.toString());
				bufferedWriter.close();
				fileWriter.close();
				
				return "A Ticket is Created...Please take your ticket "+ tNumber;
			
			}
			catch(IOException e){
				return "Sorry, ticket generation currently unavailable. Please call for assistance?";
				
			}
		}//end else
		
	} //end create Ticket
		public  String loadTicket(int tNumber){
			String fileName2 = "Ticket/"+tNumber+".dat";
			File ticket2 = new File(fileName2);
			if(ticket2.exists()){
				try{
					FileReader fileReader = new FileReader(fileName2);
					BufferedReader bufferedReader = new BufferedReader(fileReader);
					entranceTime = Long.parseLong(bufferedReader.readLine()); 
					paymentTime = Long.parseLong(bufferedReader.readLine()); 
					ticketStatus = bufferedReader.readLine();
					balance = Double.parseDouble(bufferedReader.readLine()); 
					bufferedReader.close();
					fileReader.close();
					return "The Ticket has loaded successfully.";
				}//end try
				catch(IOException e){
					
					return "The Ticket could not be read.";
				}	//end catch		
			}
			else {
			
				return "This Ticket hasn't been registered.";
			}
		
		
		
		}
		
		
	
	}

Recommended Answers

All 6 Replies

hard to extract the entrance time from the file

Are you gettting compiler errors? Please post the errors
or please explain what the problem is.

Where is the enterListener() method? I see this:
enterButton.addActionListener(new enterListener());
which looks like enterListener is a class and not a method. You don't use new with a method.

Are you gettting compiler errors? Please post the errors
or please explain what the problem is.

Where is the enterListener() method? I see this:
enterButton.addActionListener(new enterListener());
which looks like enterListener is a class and not a method. You don't use new with a method.

its on the paymentnewGui class please look up i am using instantiation on ticket class to access that method passing ticket number as parameter.

Are you gettting compiler errors? Please post the errors.

am using instantiation on ticket class to access that method passing ticket number as parameter.

Please give the line numbers where the problem is.
What variable is the ticket number in?
What method are you calling?

The more info you give the better. Don't expect me to search through you code to find something that you refer to so vaguely. Use the names of parts of the program like the correctly spelled names of the class, method and variables.

There is no paymentnewGui or ticket class. Case is important.

Are you gettting compiler errors? Please post the errors.


Please give the line numbers where the problem is.
What variable is the ticket number in?
What method are you calling?

The more info you give the better. Don't expect me to search through you code to find something that you refer to so vaguely. Use the names of parts of the program like the correctly spelled names of the class, method and variables.

There is no paymentnewGui or ticket class. Case is important.

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For inpu
t string: "null"
        at java.lang.NumberFormatException.forInputString(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at java.lang.Long.parseLong(Unknown Source)
        at Ticket.loadTicket(Ticket.java:63)
        at PaymentNewGUI$enterListener.actionPerformed(PaymentNewGUI.java:79)
        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 Sour
ce)
        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.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)

ticket number is in Ticket class as int tNumber. on paymentNew GUI class ticket number is entered into textfield as ticketID which is casted to int and this is passed in the method.

I am calling loadTicket method from Ticket class by method of instantiation and passing ticketID entered by user as arguement.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Date;
import java.lang.*;

public class PaymentNewGUI extends JFrame{
	
	private JButton tenP, twentyP, fiftyP, onePound, twoPound;
	JTextField ticketID;
	
	double amountDue = 0.00;
	double amountInserted;
	Label lEntranceTime;
	Label lPaymentTime;
	Label lDue;
	Label lInserted;
	Label lMessage = new Label();
	Container content;
	JPanel ticketPanel;
	
	/***Key Variable***/
	long entranceTime;
	long paymentTime;
	String ticketStatus;
	double balance;
	
	
		
	
	public PaymentNewGUI() {
		
		lEntranceTime = new Label("Entrance Time: " );
		lPaymentTime = new Label("Payment Time: " );
		lDue = new Label("Amount Due:   \u00A3" + amountDue);
		lInserted = new Label("Amount Instered:   \u00A3" + amountInserted);
		
		
		content = getContentPane();
		content.setLayout(new BorderLayout());
		this.setTitle("Enter Ticket Information");
	 	
		Label lTicketID = new Label("Enter Ticket No:");
		JButton enterButton = new JButton("Enter");
		enterButton.addActionListener(new enterListener());
		ticketID = new JTextField(9);
		ticketPanel = new JPanel();
    	ticketPanel.setLayout(new GridLayout(2,0));
		ticketPanel.add(lTicketID);
		ticketPanel.add(ticketID);
		ticketPanel.add(enterButton);
		ticketPanel.setVisible(true);
		content.add(ticketPanel, BorderLayout.NORTH);
		
		/*********************Ticket Number Listener************************************/
		
		
		/*********************Label Addition to Frame Content***********************/
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(4,0));
		labelPanel.setVisible(true);
		labelPanel.add(lEntranceTime);
		labelPanel.add(lPaymentTime);
		labelPanel.add(lDue);
		labelPanel.add(lInserted);
		content.add(labelPanel, BorderLayout.WEST);
		/*********************Label Only*************************/
		
		
	}//end PaymentNewGUI constructor
	
	
	public class enterListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			
			Ticket ticket = new Ticket();
			int ticketIDNumber = Integer.parseInt(ticketID.getText());
			String loadMessage = ticket.loadTicket(ticketIDNumber);
			lMessage.setText(loadMessage);
			ticketPanel.add(lMessage, BorderLayout.SOUTH);
			
			entranceTime = Ticket.entranceTime;
			paymentTime = Ticket.paymentTime;
			ticketStatus = Ticket.ticketStatus;
		    balance = Ticket.balance;
			
			if (loadMessage.equals("The Ticket has loaded successfully.")){
				
			
				lEntranceTime.setText("Entrance Time: " + entranceTime);
				
	    	} else  {
	    				System.out.println("Incorrect Ticket Information Given!");
		}

	
		}}

	
	public static void main(String args[]) {
		PaymentNewGUI frame = new PaymentNewGUI();
		frame.pack();
		frame.setVisible(true);
		frame.setSize(400,400);
		}
	
	
	
	
}





import java.util.Date;
import java.lang.*;
import java.io.*;
import java.util.Calendar;

public class Ticket {
	public TicketInfo x;
	public int tNumber;
	public static long entranceTime;
	public static long paymentTime;
	public static String ticketStatus;
	public static double balance;
	/********************************************************************************************/
	public String createTicket(){
		
	tNumber = (int)(Math.random() * 999999)+100000;


	
		File dir = new File("Ticket");
		boolean exists = dir.exists();
		if (!exists){
			dir.mkdir();
		} // end if

		TicketInfo x = new TicketInfo(entranceTime, paymentTime, ticketStatus, balance);
		 
   

		String fileName = "Ticket/"+tNumber+".dat";
		File ticket = new File(fileName);
		
		if(ticket.exists()){
			//generateTicketNumber();
			return "Try again";
		}
		else {
			try{
				FileWriter fileWriter = new FileWriter(fileName);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(x.toString());
				bufferedWriter.close();
				fileWriter.close();
				
				return "A Ticket is Created...Please take your ticket "+ tNumber;
			
			}
			catch(IOException e){
				return "Sorry, ticket generation currently unavailable. Please call for assistance?";
				
			}
		}//end else
		
	} //end create Ticket
		public  String loadTicket(int tNumber){
			String fileName2 = "Ticket/"+tNumber+".dat";
			File ticket2 = new File(fileName2);
			if(ticket2.exists()){
				try{
					FileReader fileReader = new FileReader(fileName2);
					BufferedReader bufferedReader = new BufferedReader(fileReader);
					entranceTime = Long.parseLong(bufferedReader.readLine()); 
					paymentTime = Long.parseLong(bufferedReader.readLine()); 
					ticketStatus = bufferedReader.readLine();
					balance = Double.parseDouble(bufferedReader.readLine()); 
					bufferedReader.close();
					fileReader.close();
					return "The Ticket has loaded successfully.";
				}//end try
				catch(IOException e){
					
					return "The Ticket could not be read.";
				}	//end catch		
			}
			else {
			
				return "This Ticket hasn't been registered.";
			}
		
		
		
		}
		
		
	
	}

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For inpu
t string: "null"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at Ticket.loadTicket(Ticket.java:63)

The value being passed to parseLong at line 63 is null. Look at line 63 and see why the code passes a null value to the parseLong method.
The code should test the value of the variable on line 63 and make sure it is not null before passing it to parseLong.

The value being passed to parseLong at line 63 is null. Look at line 63 and see why the code passes a null value to the parseLong method.
The code should test the value of the variable on line 63 and make sure it is not null before passing it to parseLong.

ahhh thank you, that means in the file its NULL and cant convert to long. I see what it is. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.