Question

In: Computer Science

How do I fix the "error: bad operand types for binary operator '*' " in my...

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

}

});

}

}

Solutions

Expert Solution

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


Related Solutions

Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
Although Java has the rule that the left operand of every binary operator is evaluated before...
Although Java has the rule that the left operand of every binary operator is evaluated before the right operand, most languages give the compiler the freedom to choose which operand is evaluated first. When expressions have side effects, the value of the expression can be different depending upon which order is used. Give an example in C++ of an expression whose value depends upon the evaluation order. Show the orders that produce different values and the values they produce. Explain...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[]...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[] cannot be converted to int int size = new int [start]; //Java Program import java.util.Scanner; public class Test{ public static void main(String []args) { int start, num; System.out.println("Enter the number of elements in a array : "); start = STDIN_SCANNER.nextInt(); int size = new int [start]; System.out.println("Enter the elements of array where last element must be '0' : "); for(int i = 0; i...
I am getting an error at linen 57 and can't figure out how to fix it....
I am getting an error at linen 57 and can't figure out how to fix it. // Java program to read a CSV file and display the min, max, and average of numbers in it. import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main {     // method to determine and return the minimum number from the array     public static int minimum(int numbers[])     {         int minIdx = 0;         for(int i=1;i<numbers.length;i++)         {             if((minIdx...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
Why my net pay is always 0, and how can I fix it ? here's the...
Why my net pay is always 0, and how can I fix it ? here's the code #include <iostream> #include <fstream> #include <iomanip> #include <cmath> using namespace std; int main() { ofstream out; out.open("myData.txt"); string fname;    cout << "name?" << endl; cin >> fname; double salary; cout << "salary?" << endl; cin >> salary;    double fedTax = salary * 15 / 100; double stateTax = salary* 3.5 / 100; double SST = salary * 5.75 / 100; double...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
What is Type I error? How do we correct for Type I error? What happens when...
What is Type I error? How do we correct for Type I error? What happens when we correct for Type I error? What is Type II error? How do we correct for Type II error? What happens when we correct for Type II error? How can we correct for both Type I and Type II error at the same time? Which error is considered the worst type of error to commit?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT