473,434 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Java GUI calculator

2
Hello! I just can't figure out how can i do few operations in one transaction (ex: 5-4+1) i would appreciate if somebody could point on my mistakes. Here is my code. for some reason after pressing second operator it would replace the first one....any ideas?



Expand|Select|Wrap|Line Numbers
  1. class Operators implements ActionListener
  2. {        
  3. int num1=0;
  4. int num2=0;
  5. int op=0;
  6. int a=1;
  7.  
  8.  
  9. public void actionPerformed(ActionEvent e)
  10. {
  11.  
  12. Object src = e.getSource();
  13. String valuestr=n1Txt.getText();
  14. int valueint= Integer.parseInt(valuestr);
  15.  
  16. if(src==bmin)
  17. {op=1;//-
  18. if(a==1){
  19. a=2;
  20. num1=valueint;
  21. }
  22. else if(a==2){
  23. a=3;
  24. num2=valueint;
  25. num1=num1-num2;}
  26. else if(a==3){
  27. a=2;
  28. num2=valueint;
  29. num1=num1-num2;    
  30. }                        
  31. }
  32.  
  33. if(src==bpl)
  34. {op=2;//+
  35. if(a==1){
  36. a=2;
  37. num1=valueint;
  38. }
  39. else if(a==2){
  40. a=3;
  41. num2=valueint;
  42. num1=num1+num2;}
  43. else if(a==3){
  44. a=2;
  45. num2=valueint;
  46. num1=num1+num2;    
  47. }                        
  48. }
  49.  
  50. if(src==bmult)
  51. {op=3;//*
  52. if(a==1){
  53. a=2;
  54. num1=valueint;}
  55. else if(a==2){
  56. a=3;
  57. num2=valueint;
  58. num1=num1*num2;}
  59. else if(a==3){
  60. a=2;
  61. num2=valueint;
  62. num1=num1*num2;    
  63. }                        
  64. }
  65.  
  66. if(src==bdev)
  67. {op=4;// /
  68. if(a==1){
  69. a=2;
  70. num1=valueint;}
  71. else if(a==2){
  72. a=3;
  73. num2=valueint;
  74. num1=num1/num2;}
  75. else if(a==3){
  76. a=2;
  77. num2=valueint;
  78. num1=num1/num2;    
  79. }                        
  80. }
  81.  
  82. n1Txt.setText(""); // where n1Txt is the display field
  83. if(src==beq){
  84. switch (op) {
  85.     case 1 :num2=valueint;
  86.             num1=num1-num2;
  87.     break;
  88.     case 2 :num2=valueint;
  89.             num1=num1+num2;
  90.     break;
  91.     case 3 :num2=valueint;
  92.             num1=num1*num2;
  93.     break;
  94.     case 4 :num2=valueint;
  95.             num1=num1/num2;
  96.     break;
  97.  
  98. }
  99. String result = Integer.toString(num1);
  100. n1Txt.setText(result);
  101. }}
Jun 1 '09 #1
2 3927
r035198x
13,262 8TB
Better format your code properly and post it wrapped in code tags.
Jun 2 '09 #2
Yukka
2
Hello! I just can't figure out how can i do few operations in one transaction (ex: 5-4+1) i would appreciate if somebody could point on my mistakes. Here is my code. for some reason after pressing second operator it would replace the first one....any ideas?



Expand|Select|Wrap|Line Numbers
  1.  int num1=0; 
  2.     int num2=0; 
  3.     int op=0; 
  4.     int a=1; 
  5.  
  6.  
  7.     public void actionPerformed(ActionEvent e) 
  8.     { 
  9.  
  10.     Object src = e.getSource(); 
  11.     String valuestr=n1Txt.getText(); 
  12.     int valueint= Integer.parseInt(valuestr)
  13.     if(src==bmin) 
  14.     {op=1;  //- 
  15.         if(a==1){ 
  16.          a=2; 
  17.                     num1=valueint; } 
  18.  
  19.         else if(a==2) 
  20.         {a=3; 
  21.         num2=valueint; 
  22.         num1=num1-num2;} 
  23.  
  24.         else if(a==3) 
  25.         {a=2; 
  26.         num2=valueint; 
  27.         num1=num1-num2;}         
  28.     } 
  29.  
  30.     if(src==bpl) 
  31.     {op=2;//+ 
  32.         if(a==1) 
  33.          {a=2; 
  34.          num1=valueint;} 
  35.  
  36.          else if(a==2) 
  37.         {a=3; 
  38.         num2=valueint; 
  39.         num1=num1+num2;} 
  40.  
  41.         else if(a==3) 
  42.         {a=2; 
  43.         num2=valueint; 
  44.         num1=num1+num2;}  
  45.      }     
  46.  
  47.     if(src==bmult) 
  48.     {op=3;//* 
  49.         If(a==1) 
  50.         {a=2; 
  51.         num1=valueint;} 
  52.  
  53.         else if(a==2) 
  54.         {a=3; 
  55.         num2=valueint; 
  56.         num1=num1*num2;} 
  57.  
  58.         else if(a==3) 
  59.         {a=2; 
  60.         num2=valueint; 
  61.         num1=num1*num2;}
  62.     } 
  63.  
  64.     if(src==bdev) 
  65.     {op=4;// / 
  66.         if(a==1) 
  67.         { a=2; 
  68.         num1=valueint;} 
  69.  
  70.         else if(a==2)  
  71.         { a=3; 
  72.         num2=valueint; 
  73.         num1=num1/num2;} 
  74.  
  75.         else if(a=3) 
  76.         {a=2; 
  77.         num2=valueint; 
  78.         num1=num1/num2;} 
  79.     }     
  80.  
  81. n1Txt.setText(""); // where n1Txt is the display field 
  82.     if(src==beq){ 
  83.     switch (op) { 
  84.         case 1 :num2=valueint; 
  85.                     num1=num1-num2; 
  86.             break; 
  87.             case 2 :num2=valueint; 
  88.                     num1=num1+num2; 
  89.             break; 
  90.             case 3 :num2=valueint; 
  91.                     num1=num1*num2; 
  92.             break; 
  93.             case 4 :num2=valueint; 
  94.                     num1=num1/num2; 
  95.             break; 
  96.  
  97.     } 
  98.  
  99. String result = Integer.toString(num1); 
  100. n1Txt.setText(result); 
  101.                 } 
  102.  
[quote=Yukka;3491355]
Jun 2 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Dr. Mercury | last post by:
Greetings to all - Someone suggested that Java might be the answer for a little conversion program I need. It's pretty simple, really. Just take a column of numbers: 139 259 433 637
3
by: PieMan2004 | last post by:
Hi, ive been looking for a solid java community to help me when im tearing out my hair :) Basically ive constructed a GUI that has to represent the same look and functions of the typical windows...
2
by: erekose666 | last post by:
I need some help here if anyone is willing. I need to take this program and modify it to do something a little different. First things first, though. If you run this code like it is right now, it...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
5
by: erekose666 | last post by:
I have this program right now, but I need to modify it to output to the JTextPane. What I mean is, when someone clicks on the 1 button, it needs to output to the TextArea a 1. None of the add,...
15
by: colinNeedsJavaHelp | last post by:
I am attempting to program a very basic calculator. The program simply needs to prompt the use to input the computation i.e. 2 * 5 The program needs to compute this and display the result as "The...
11
by: LolaT | last post by:
I have just started using java and I'm trying to make a simple calculator, but I'm confused as to why I keep getting a warning message. import java.util.Scanner; public class Calculator { ...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
2
by: jtanz0 | last post by:
Im fairly new to java programming although i have some experience in python. I have to create a calculator program (command line) which takes as input a mathematical expression consisting of...
1
by: Techno3000 | last post by:
import java.io.*; import java.awt.*; import hsa.Console; public class Calculator { static Console c; public static void main (String args) throws IOException
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.