Whenever I press the equals, it gives me the following error log:Įxception in thread "AWT-EventQueue-0" : For input string: "9.0+9"Īt (FloatingDecimal.java:2043)Īt (FloatingDecimal.java:110)Īt (Double.java:538)Īt Calculator.mathmematical_operation(Calculator.java:27)Īt Calculator.actionPerformed(Calculator.java:206)Īt (AbstractButton.java:2022)Īt $Handler.actionPerformed(AbstractButton.java:2346)Īt (DefaultButtonModel.java:402)Īt (DefaultButtonModel.java:259)Īt .BasicButtonListener.mouseReleased(BasicButtonListener.java:252)Īt (Component.java:6525)Īt (JComponent.java:3324)Īt (Component.java:6290)Īt (Container.java:2234)Īt (Component.java:4881)Īt (Container.java:2292)Īt (Component.java:4703)Īt (Container.java:4898)Īt (Container.java:4533)Īt (Container.java:4462)Īt (Container. Can you solve this real interview question Basic Calculator - Given a string s representing a valid expression, implement a basic calculator to evaluate it. Numb = Double.parseDouble(result.getText()) Public static void mathmematical_operation()Īnswer = numb + Double.parseDouble(result.getText()) Īnswer = numb - Double.parseDouble(result.getText()) Īnswer = numb * Double.parseDouble(result.getText()) Īnswer = numb / Double.parseDouble(result.getText()) I have tried to make the calculations, but I ran into an error where the equals is not responding properly. tPreferredSize (new Dimension (500,500)) įEquals = new JButton void actionPerformed(ActionEvent e) MyTitleFont = new Font(Font.MONOSPACED, Font.PLAIN,40) Private JButton FSum, FMinus, FMulti, FDivide, FComma, FEquals Private JButton Btn1, Btn2, Btn3, Btn4, Btn5, Btn6, Btn7, Btn8, Btn9 Import class Calculator extends JFrame implements ActionListener For now, I need advice on how I can make my buttons respond like a calculator. I also have to organize the calculator with grids and panels, but I'll leave it for last. I can't figure out how to make my buttons respond to calculations, such as pressing one button and then an arithmetic button (sum, minus, multiply, divide, equals) so that when I press a different button (or the same one), it gives me the calculated result. System.in which represents STDIN, the InputStreamReaderĪnd the BufferedReader classes to read from the input channel.I'm struggling with making a Java calculator. The interesting difference here is the use of We ask the user to type the values in during the execution of the program.īufferedReader br = new BufferedReader(new InputStreamReader(System.in)) In this alternative soluton, instead expecting the operands and the operator on the command line, Throw new (".") will raise an exception in case the user supplied an operator we don't handle. Write a program to find the sum, difference, product, quotient and remainder of two numbers passed as. If has its condition in parentheses and the code in curly braces.Įlse if allows us to attach more, alternative conditions. Java calculator program using command-line arguments. String is used to declare a variable as string. Int is used to decalare a variable as integer. Integer.parseInt can convert a string into an integer number. The args array will hold the values passed on the commad line. Is also 7 as Java will only keep the integer part of the division. For this we need to but it in quotes:įinally we also have to accept that the division as it is in this code will work as an integer division. In order to avoid this we need to tell the shell to pass the * as it is. This problem is caused by the shell interfering with our businees. To parse the string "ArgsCalculator.java" into an integer. That in turn will trigger an exception on our code as it tries In the Java Control Panel, click the Advanced tab. Java ArgsCalculator 7 ArgsCalculator.java ArgsCalculator.class HelloWorld.java 3Īssuming we have those 3 files in the current directory. So the shell will actually call something like this: That's because the * on the command line is interpreted by the shell as a wide-card character and it is replaced by the names of all the files in the current directory. If we would like to multiply numbers we have to be careful.Įxception in thread "main" : For input string: "ArgsCalculator.java"Īt (NumberFormatException.java:65)Īt (Integer.java:580)Īt (Integer.java:615)Īt ArgsCalculator.main(ArgsCalculator.java:6) Will compile the Java clode and create a file called ArgsCalculator.class In this solution we receive the 2 numbers and the operator on the command line.
0 Comments
Leave a Reply. |