In: Computer Science
How do I fix the "error: bad operand types for binary operator '*' " in my code?
What I am trying to do:
double TotalPrice = TicketPrice * NoOfTickets;
My code:
import javax.swing.*;
/*provides interfaces and classes for different events by AWT
components*/
import java.awt.event.*;
import javax.swing.JOptionPane;
//TicketReservation.java
class TicketReservation
{
public static void main(String args[])
{
/*Declare JFrame for place controls.*/
JFrame f= new JFrame("Movie Ticket Reservation");
/*Declare JLabels*/
JLabel MovieTitle, NoOfTickets, ShowTime, TicketPrice;
/*Declare JTextFields*/
JTextField txtMovieTitle, txtNoOfTickets, txtShowTime, txtTicketPrice;
/*Declare JButton "Reserve*/
JButton Reserve =new JButton("Reserve");
/*Declare JTextArea*/
JTextArea txtaReserve;
/*Creating JLabel object "MovieTitle"*/
MovieTitle = new JLabel("Movie Title:");
MovieTitle.setBounds(20,50, 200,30);
/*Creating JLabel object NoOfTickets*/
NoOfTickets = new JLabel("Number of Tickets:");
/*X, Y, Widht, Hight of control*/
NoOfTickets.setBounds(20,100, 200,30);
/*Creating JLabel object "ShowTime*/
ShowTime = new JLabel("Show Time:");
ShowTime.setBounds(20,150, 200,30);
/*Creating JLabel object TicketPrice*/
TicketPrice=new JLabel("Ticket Price:");
TicketPrice.setBounds(20,200, 200,30);
/*constructing text area*/
txtaReserve = new JTextArea();
txtaReserve.setBounds(20,300, 400,150);
/*creating text field objects*/
txtMovieTitle = new JTextField();
txtMovieTitle.setBounds(200,50, 200,30);
txtNoOfTickets = new JTextField("");
txtNoOfTickets.setBounds(200,100, 200,30);
txtShowTime = new JTextField("");
txtShowTime.setBounds(200,150, 200,30);
txtTicketPrice = new JTextField("");
txtTicketPrice.setBounds(200,200, 200,30);
Reserve.setBounds(100,450,100,30);
/*Adding controls into JFrame*/
f.add(MovieTitle);
f.add(NoOfTickets);
f.add(ShowTime);
f.add(TicketPrice);
f.add(txtaReserve);
f.add(txtMovieTitle);
f.add(txtNoOfTickets);
f.add(txtShowTime);
f.add(txtTicketPrice);
f.add(Reserve);
//*Set JFrame size and properties*/
f.setSize(500,600);
f.setLayout(null);
f.setVisible(true);
/*Used action lister to calculate and display result
in text area, when user click Reserve button.*/
Reserve.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try
{
/*Get the input from text fields*/
String MovieTitle = txtMovieTitle.getText();
int NoOfTickets = Integer.parseInt(txtNoOfTickets.getText());
String ShowTime = txtShowTime.getText();
double TicketPrice = Double.parseDouble(txtTicketPrice.getText());
}
catch (Exception exe) {
JOptionPane.showMessageDialog(null, "Error: Invalid input",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
/*Calculate TotalPrice*/
double TotalPrice = TicketPrice * NoOfTickets;
/*Output string*/
String str = "Movie Title: " + MovieTitle + "\nNumber of Tickets: "
+ NoOfTickets + "\nShowTime: " + ShowTime + "\nTicket Price: " +
TicketPrice + "\n Total Price of Reservation: "+TotalPrice;
/*Set the output into textarea*/
txtaReserve.setText(str);
}
});
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//TicketReservation.java
class TicketReservation
{
public static void main(String args[])
{
/* Declare JFrame for place controls. */
JFrame f = new JFrame("Movie Ticket Reservation");
/* Declare JLabels */
JLabel MovieTitle, NoOfTickets, ShowTime, TicketPrice;
/* Declare JTextFields */
JTextField txtMovieTitle, txtNoOfTickets, txtShowTime, txtTicketPrice;
/* Declare JButton "Reserve */
JButton Reserve = new JButton("Reserve");
/* Declare JTextArea */
JTextArea txtaReserve;
/* Creating JLabel object "MovieTitle" */
MovieTitle = new JLabel("Movie Title:");
MovieTitle.setBounds(20, 50, 200, 30);
/* Creating JLabel object NoOfTickets */
NoOfTickets = new JLabel("Number of Tickets:");
/* X, Y, Widht, Hight of control */
NoOfTickets.setBounds(20, 100, 200, 30);
/* Creating JLabel object "ShowTime */
ShowTime = new JLabel("Show Time:");
ShowTime.setBounds(20, 150, 200, 30);
/* Creating JLabel object TicketPrice */
TicketPrice = new JLabel("Ticket Price:");
TicketPrice.setBounds(20, 200, 200, 30);
/* constructing text area */
txtaReserve = new JTextArea();
txtaReserve.setBounds(20, 300, 400, 150);
/* creating text field objects */
txtMovieTitle = new JTextField();
txtMovieTitle.setBounds(200, 50, 200, 30);
txtNoOfTickets = new JTextField("");
txtNoOfTickets.setBounds(200, 100, 200, 30);
txtShowTime = new JTextField("");
txtShowTime.setBounds(200, 150, 200, 30);
txtTicketPrice = new JTextField("");
txtTicketPrice.setBounds(200, 200, 200, 30);
Reserve.setBounds(100, 450, 100, 30);
/* Adding controls into JFrame */
f.add(MovieTitle);
f.add(NoOfTickets);
f.add(ShowTime);
f.add(TicketPrice);
f.add(txtaReserve);
f.add(txtMovieTitle);
f.add(txtNoOfTickets);
f.add(txtShowTime);
f.add(txtTicketPrice);
f.add(Reserve);
//*Set JFrame size and properties*/
f.setSize(500, 600);
f.setLayout(null);
f.setVisible(true);
/*
* Used action lister to calculate and display result in text area, when user
* click Reserve button.
*/
Reserve.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
/* Get the input from text fields */
String MovieTitle = txtMovieTitle.getText();
int NoOfTickets = Integer.parseInt(txtNoOfTickets.getText());
String ShowTime = txtShowTime.getText();
double TicketPrice = Double.parseDouble(txtTicketPrice.getText());
double TotalPrice = TicketPrice * NoOfTickets;
/* Output string */
String str = "Movie Title: " + MovieTitle + "\nNumber of Tickets: " + NoOfTickets + "\nShowTime: "
+ ShowTime + "\nTicket Price: " + TicketPrice + "\n Total Price of Reservation: " + TotalPrice;
/* Set the output into textarea */
txtaReserve.setText(str);
}
catch (Exception exe) {
JOptionPane.showMessageDialog(null, "Error: Invalid input", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
/* Calculate TotalPrice */
}
});
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME