I\'m getting error ArithmeticOperation.java:15: error: invalid method declaratio
ID: 3647178 • Letter: I
Question
I'm getting errorArithmeticOperation.java:15: error: invalid method declaration; return type required
public arithmeticOperationJOptionPane() {
^
code :
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class ArithmeticOperation extends JFrame {
private JButton buttons[];
private String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};
public arithmeticOperationJOptionPane() {
super("Arithmetic Operation using JOptionPane");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[4];
Container pane = getContentPane();
setContentPane(pane);
GridLayout grid = new GridLayout(2,2);
pane.setLayout(grid);
for(int count=0; count<buttons.length; count++) {
buttons[count] = new JButton(operation[count]);
pane.add(buttons[count]);
}
buttons[0].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 + num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){ //Catch the error if the user inputs a non-integer value
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[1].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 - num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[2] which is multiplication
buttons[2].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 * num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[3] which is division
buttons[3].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 / num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[0].setMnemonic('A');
buttons[1].setMnemonic('S');
buttons[2].setMnemonic('M');
buttons[3].setMnemonic('D');
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
setResizable(false); //Fix window height and width
}
//Main Method
public static void main (String[] args) {
arithmeticOperationJOptionPane pjtf = new arithmeticOperationJOptionPane();
}
}
Explanation / Answer
Every method except constructors must have a return type or void specified; if not, this error will arise: max(int i, int j) { ... } The error frequently occurs because it is easy to misspell the name of a constructor; the compiler then thinks that it is a normal method without a return type: class MyClass { MyClass(int i) { ... } Myclass(int i, int j) { ... } // Error because of the lowercase c }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.