Answer the question
In order to leave comments, you need to log in
How to give roman numerals arabic value so that there is no error in java calculator?
Help me please. How can I make buttons with Roman symbols read with each other, and there would be no error with + - * /? One of the tasks: The calculator can work with both Arabic (1,2,3,4,5 ...) and with Roman (I,II,III,IV,V…) numerals; the calculator can only work with Arabic or Roman numerals at the same time, when the user enters a string like 3 + II, the calculator should throw an exception and stop working.
package calculator_jm;
import java.awt.*;
import javax.swing.*;
/**
*
* @author Полина
*/
public class Calculator_JM {
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton buttonI = new JButton("I");
JButton buttonII = new JButton("II");
JButton buttonIII = new JButton("III");
JButton buttonIV = new JButton("IV");
JButton buttonV = new JButton("V");
JButton buttonVI = new JButton("VI");
JButton buttonVII = new JButton("VII");
JButton buttonVIII = new JButton("VIII");
JButton buttonIX = new JButton("IX");
JButton buttonEqual = new JButton("=");
JButton buttonBack = new JButton("C");
JButton buttonPlus = new JButton("+");
JButton buttonMinus = new JButton("-");
JButton buttonMultiply = new JButton("*");
JButton buttonDivide = new JButton("/");
JPanel windowContent = new JPanel();
JTextField displayField = new JTextField(30);
Calculator_JM() {
BorderLayout bl = new BorderLayout();
windowContent.setLayout(bl);
//менеджер расположения
windowContent.add("North", displayField);
displayField.setEditable(false);
//панель с менеджером расположения
JPanel p1 = new JPanel();
GridLayout gl = new GridLayout(9,3);
p1.setLayout(gl);
p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(buttonI);
p1.add(buttonII);
p1.add(buttonIII);
p1.add(buttonIV);
p1.add(buttonV);
p1.add(buttonVI);
p1.add(buttonVII);
p1.add(buttonVIII);
p1.add(buttonIX);
p1.add(button0);
p1.add(buttonBack);
p1.add(buttonEqual);
windowContent.add("Center", p1);
//кнопки для знаков
JPanel p2 = new JPanel();
GridLayout gl2 = new GridLayout(4,1);
p2.setLayout(gl2);
p2.add(buttonPlus);
p2.add(buttonMinus);
p2.add(buttonMultiply);
p2.add(buttonDivide);
//панель сместить вправо
windowContent.add("East", p2);
//создать и добавить все во фрейм
JFrame frame = new JFrame("Calculator_JM");
frame.setContentPane(windowContent);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//размер окна и видимость
frame.pack();
frame.setVisible(true);
//экземпляр слушател и регистрация
CalculatorEngine calculatorEngine = new CalculatorEngine(this);
button0.addActionListener(calculatorEngine);
button1.addActionListener(calculatorEngine);
button2.addActionListener(calculatorEngine);
button3.addActionListener(calculatorEngine);
button4.addActionListener(calculatorEngine);
button5.addActionListener(calculatorEngine);
button6.addActionListener(calculatorEngine);
button7.addActionListener(calculatorEngine);
button8.addActionListener(calculatorEngine);
button9.addActionListener(calculatorEngine);
buttonI.addActionListener(calculatorEngine);
buttonII.addActionListener(calculatorEngine);
buttonIII.addActionListener(calculatorEngine);
buttonIV.addActionListener(calculatorEngine);
buttonV.addActionListener(calculatorEngine);
buttonVI.addActionListener(calculatorEngine);
buttonVII.addActionListener(calculatorEngine);
buttonVIII.addActionListener(calculatorEngine);
buttonIX.addActionListener(calculatorEngine);
buttonBack.addActionListener(calculatorEngine);
buttonPlus.addActionListener(calculatorEngine);
buttonMinus.addActionListener(calculatorEngine);
buttonMultiply.addActionListener(calculatorEngine);
buttonDivide.addActionListener(calculatorEngine);
buttonEqual.addActionListener(calculatorEngine);
}
public static void main(String[] args) {
Calculator_JM calc = new Calculator_JM();
}
}
--------------------------------------(новый класс)
package calculator_jm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class MyDrawingPad implements MouseMotionListener, ActionListener {
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
-----------------------------------------------------------------------
package calculator_jm;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
/**
*
* @author Полина
*/
public class CalculatorEngine implements ActionListener {
Calculator_JM parent;
char selectedAction = ' '; //+ - * /
int currentResult = 0;
int roman = 1;
String s;
CalculatorEngine(Calculator_JM parent){
this.parent = parent;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
String dispFieldText = parent.displayField.getText();
double displayValue = 0;
if(!"".equals(dispFieldText)){
displayValue = Double.parseDouble(dispFieldText);}
Object src = e.getSource(); //для каждого деяствия кнопочки
if (src == parent.buttonPlus){
selectedAction = '+';
currentResult = (int) displayValue;
parent.displayField.setText(""); }
else if (src == parent.buttonMinus){
selectedAction = '-';
currentResult = (int) displayValue;
parent.displayField.setText(""); }
else if (src == parent.buttonMultiply){
selectedAction = '*';
currentResult = (int) displayValue;
parent.displayField.setText(""); }
else if (src == parent.buttonDivide){
selectedAction = '/';
currentResult = (int) displayValue;
parent.displayField.setText(""); }
else if (src == parent.buttonBack){
selectedAction = 'C';
currentResult = (int) displayValue;
parent.displayField.setText("");}
else if (src == parent.buttonEqual){
if (selectedAction == '+'){
currentResult += displayValue;
parent.displayField.setText("" + currentResult);}
else if (selectedAction == '-'){
currentResult -= displayValue;
parent.displayField.setText("" + currentResult);}
else if (selectedAction == '*'){
currentResult *= displayValue;
parent.displayField.setText("" + currentResult);}
else if (selectedAction == '/'){
currentResult /= displayValue;
parent.displayField.setText("" + currentResult);}
else if (selectedAction == 'C'){
currentResult = (int) displayValue;
parent.displayField.setText("" + currentResult);}
} else {
String clickedButtonLabel = clickedButton.getText();
parent.displayField.setText(dispFieldText + clickedButtonLabel);}
}}
Answer the question
In order to leave comments, you need to log in
Try this
double displayValue = 0;
if (!"".equals(dispFieldText)) {
try {
displayValue = Double.parseDouble(dispFieldText);
}catch (NumberFormatException e1){
HashMap<String,Integer> map = new HashMap<>();
map.put("I",1);
map.put("II",2);
map.put("III",3);
map.put("IV",4);
map.put("V",5);
map.put("VI",6);
map.put("VII",7);
map.put("VIII",8);
map.put("IX",9);
displayValue = Double.parseDouble(String.valueOf(map.get(dispFieldText)));
}
}
Object src = e.getSource(); //для каждого деяствия кнопочки
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question