Sign in with
Sign up | Sign in
Your question
Solved

Calculator program - JAVA

Last response: in Applications
Share
March 5, 2011 2:41:44 PM

Hey everyone, need a little help. For some reason I am horrible at file I/O and always have trouble with it. Well my problem, I have a calculator class that shows a user a menu, they then pick a choice and enter 2 numbers and get the sum from w/e choice they picked. I have that all up and working. The last part of the assignment is to log every choice the user picks until he ends the program. For example the log file would look like this:

4+5=9
3-5=-2
7*7=49
ect
ect
ect

Just a file that someone can go in at the end of the program and just see a simple view of what he entered in order and the results. I get the file to be created but when I open it, it is blank... Here's the code:

  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class Calculator {
  5.  
  6. public int add(int num1, int num2){
  7.  
  8. int sum;
  9. sum = num1 + num2;
  10.  
  11. return sum;
  12. }
  13.  
  14. public int sub(int num1, int num2){
  15.  
  16. int sum;
  17. sum = num1 - num2;
  18.  
  19. return sum;
  20. }
  21.  
  22. public int multi(int num1, int num2){
  23.  
  24. int sum;
  25. sum = num1 * num2;
  26.  
  27. return sum;
  28. }
  29.  
  30. public int div(int num1, int num2){
  31.  
  32. int sum;
  33. sum = num1 / num2;
  34.  
  35. return sum;
  36. }
  37.  
  38. public int mod(int num1, int num2){
  39.  
  40. int sum;
  41. sum = num1 % num2;
  42.  
  43. return sum;
  44. }
  45.  
  46. public int pow(int base, int exp){
  47.  
  48. int sum = 1;
  49.  
  50. if (exp == 0)
  51. {
  52. sum = 1;
  53. }
  54.  
  55. while (exp > 0)
  56. {
  57. sum = sum * base;
  58. exp--;
  59.  
  60. }
  61.  
  62.  
  63. return sum;
  64.  
  65. }
  66.  
  67. public static void main(String[] args) {
  68.  
  69. int choice;
  70. int x;
  71. int y;
  72. int sum;
  73.  
  74.  
  75. Calculator calc = new Calculator();
  76.  
  77.  
  78. do
  79. {
  80. System.out.println("Calculator Program");
  81. System.out.println("--------------------\n");
  82. System.out.println("1. Add");
  83. System.out.println("2. Subtract");
  84. System.out.println("3. Multiply");
  85. System.out.println("4. Divide");
  86. System.out.println("5. Mod");
  87. System.out.println("6. Power");
  88. System.out.println("99. End Program\n");
  89. System.out.println("Enter Choice: ");
  90.  
  91. Scanner input = new Scanner(System.in);
  92. choice = input.nextInt();
  93.  
  94. while ((choice < 1 || choice > 6) && choice != 99)
  95. {
  96. System.out.println("Please enter a 1, 2, 3, 4, 5, or 6: " );
  97. choice = input.nextInt();
  98. }
  99.  
  100. try
  101. {
  102. FileWriter fstream = new FileWriter("calclog.txt");
  103. BufferedWriter out = new BufferedWriter(fstream);
  104.  
  105.  
  106. switch (choice)
  107. {
  108. case 1:
  109. System.out.println("Please enter 2 numbers only: ");
  110. x = input.nextInt();
  111. y = input.nextInt();
  112. sum = calc.add(x,y);
  113. System.out.printf("The sum is %d\n\n",sum);
  114. out.write(x + "+" + y + "=" + sum);
  115. break;
  116.  
  117. case 2:
  118. System.out.println("Please enter 2 numbers only: ");
  119. x = input.nextInt();
  120. y = input.nextInt();
  121. sum = calc.sub(x,y);
  122. System.out.printf("The sum is %d\n\n",sum);
  123. out.write(x + "+" + y + "=" + sum);
  124. break;
  125.  
  126. case 3:
  127. System.out.println("Please enter 2 numbers only: ");
  128. x = input.nextInt();
  129. y = input.nextInt();
  130. sum = calc.multi(x,y);
  131. System.out.printf("The sum is %d\n\n",sum);
  132. out.write(x + "+" + y + "=" + sum);
  133. break;
  134.  
  135. case 4:
  136. System.out.println("Please enter 2 numbers only: ");
  137. x = input.nextInt();
  138. y = input.nextInt();
  139. sum = calc.div(x,y);
  140. System.out.printf("The sum is %d\n\n",sum);
  141. out.write(x + "+" + y + "=" + sum);
  142. break;
  143.  
  144. case 5:
  145. System.out.println("Please enter 2 numbers only: ");
  146. x = input.nextInt();
  147. y = input.nextInt();
  148. sum = calc.mod(x,y);
  149. System.out.printf("The sum is %d\n\n",sum);
  150. out.write(x + "+" + y + "=" + sum);
  151. break;
  152.  
  153. case 6:
  154. System.out.println("Please enter 2 numbers only: ");
  155. x = input.nextInt();
  156. y = input.nextInt();
  157. sum = calc.pow(x,y);
  158. System.out.printf("The sum is %d\n\n",sum);
  159. out.write(x + "+" + y + "=" + sum);
  160. break;
  161. }
  162. out.close();
  163. }
  164. catch(Exception e)
  165. {
  166. System.out.println("ERROR: Could not open file!");
  167. }
  168.  
  169.  
  170. }
  171. while (choice != 99);
  172. System.out.println("Ending program...");
  173.  
  174.  
  175.  
  176. }
  177. }


Thank you for any help =D.
March 5, 2011 11:32:17 PM

add an out.close as the last line of your program as that will flush the buffers then close the file, what probably is happening is that java is closing but the buffer is not being flushed before the file is closed losing the data
m
0
l
March 6, 2011 12:04:15 AM

I added the out.close but still the file is opening up blank. =/ I updated the code above.
m
0
l
Related resources
a b L Programming
March 6, 2011 12:20:31 PM

Have you tried using a PrintWriter instead of a BufferedWriter (then use print/println instead of write)? You can also call "out.flush()" before "out.close()".
m
0
l

Best solution

a b L Programming
March 6, 2011 12:29:30 PM

Actually the problem is that by default, when a file is opened, it is overwritten and since the file opening is in your do-while loop, when the user enter 99 it still opens the file, but writes nothing to it, therefore the empty file. Either open and close the file outside the loop and/or open it in "append mode" depending on the exact behavior you are looking for.

I also double-checked the PrintWriter thing and using a PrintStream would be easier since you don't need to create the FileWriter first, you can give it the "File" object or even just the file name.
Share
March 6, 2011 12:30:58 PM

good catch Zenthar on the opening the file again
m
0
l
a b L Programming
March 6, 2011 1:16:35 PM

mindless728 said:
good catch Zenthar on the opening the file again
I didn't make a career out of it for nothing ;) . It would have been clearer had the OP indented his code properly; the do-while, the try-catch and the switch-case are all at the same level.

For code cleanliness, most of the stuff in the "case" sections are just duplicates and could probably be put outside.
m
0
l
March 6, 2011 1:51:36 PM

Zenthar said:
I didn't make a career out of it for nothing ;) . It would have been clearer had the OP indented his code properly; the do-while, the try-catch and the switch-case are all at the same level.

For code cleanliness, most of the stuff in the "case" sections are just duplicates and could probably be put outside.


my career as well (though i have had one job and still in school for CS), though the indentation did not help at all
m
0
l
March 6, 2011 1:56:49 PM

I am going to try using the printstream, but you said do it outside the loop. But my slides on using the printstream shows that I need what ever to be written to the file inside the TRY block. So doesnt it have to be inside the loop? or do I put the whole loop inside the TRY block? I feel like writing to files is so easy, I don't understand why I have such a hard time with it.
m
0
l
March 6, 2011 2:07:47 PM

Alright changed some things around. Took the enter 2 numbers out of each CASE and put up top, but now when I hit 99 it asks for 2 numbers instead of ending. I tried doing the PrintStream but still no luck. And tried to indent it better, since professor says he will take a lot of points off for not properly indenting. Here's the new code:

  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. public class Calculator {
  5.  
  6. public int add(int num1, int num2){
  7.  
  8. int sum;
  9. sum = num1 + num2;
  10.  
  11. return sum;
  12. }
  13.  
  14. public int sub(int num1, int num2){
  15.  
  16. int sum;
  17. sum = num1 - num2;
  18.  
  19. return sum;
  20. }
  21.  
  22. public int multi(int num1, int num2){
  23.  
  24. int sum;
  25. sum = num1 * num2;
  26.  
  27. return sum;
  28. }
  29.  
  30. public int div(int num1, int num2){
  31.  
  32. int sum;
  33. sum = num1 / num2;
  34.  
  35. return sum;
  36. }
  37.  
  38. public int mod(int num1, int num2){
  39.  
  40. int sum;
  41. sum = num1 % num2;
  42.  
  43. return sum;
  44. }
  45.  
  46. public int pow(int base, int exp){
  47.  
  48. int sum = 1;
  49.  
  50. if (exp == 0)
  51. {
  52. sum = 1;
  53. }
  54.  
  55. while (exp > 0)
  56. {
  57. sum = sum * base;
  58. exp--;
  59.  
  60. }
  61.  
  62.  
  63. return sum;
  64.  
  65. }
  66.  
  67. public static void main(String[] args) {
  68.  
  69. int choice;
  70. int x;
  71. int y;
  72. int sum;
  73. PrintStream out;
  74.  
  75. Calculator calc = new Calculator();
  76.  
  77.  
  78. try
  79. {
  80. out = new PrintStream ("calclog.txt");
  81.  
  82. do
  83. {
  84. System.out.println("Calculator Program");
  85. System.out.println("--------------------\n");
  86. System.out.println("1. Add");
  87. System.out.println("2. Subtract");
  88. System.out.println("3. Multiply");
  89. System.out.println("4. Divide");
  90. System.out.println("5. Mod");
  91. System.out.println("6. Power");
  92. System.out.println("99. End Program\n");
  93. System.out.println("Enter Choice: ");
  94.  
  95. Scanner input = new Scanner(System.in);
  96. choice = input.nextInt();
  97.  
  98. while ((choice < 1 || choice > 6) && choice != 99)
  99. {
  100. System.out.println("Please enter a 1, 2, 3, 4, 5, or 6: " );
  101. choice = input.nextInt();
  102. }
  103.  
  104.  
  105. System.out.println("Please enter 2 numbers only: ");
  106. x = input.nextInt();
  107. y = input.nextInt();
  108.  
  109. switch (choice)
  110. {
  111. case 1:
  112. sum = calc.add(x,y);
  113. System.out.printf("The sum is %d\n\n",sum);
  114. out.println(x + "+" + y + "=" + sum);
  115. break;
  116.  
  117. case 2:
  118. sum = calc.sub(x,y);
  119. System.out.printf("The sum is %d\n\n",sum);
  120. out.println(x + "-" + y + "=" + sum);
  121. break;
  122.  
  123. case 3:
  124. sum = calc.multi(x,y);
  125. System.out.printf("The sum is %d\n\n",sum);
  126. out.println(x + "*" + y + "=" + sum);
  127. break;
  128.  
  129. case 4:
  130. sum = calc.div(x,y);
  131. System.out.printf("The sum is %d\n\n",sum);
  132. out.println(x + "/" + y + "=" + sum);
  133. break;
  134.  
  135. case 5:
  136. sum = calc.mod(x,y);
  137. System.out.printf("The sum is %d\n\n",sum);
  138. out.println(x + "%" + y + "=" + sum);
  139. break;
  140.  
  141. case 6:
  142. sum = calc.pow(x,y);
  143. System.out.printf("The sum is %d\n\n",sum);
  144. out.println(x + "^" + y + "=" + sum);
  145. break;
  146. }
  147.  
  148. }
  149.  
  150. while (choice != 99);
  151. System.out.println("Ending program...");
  152.  
  153. }
  154.  
  155. catch(Exception e)
  156. {
  157. System.out.println("ERROR: Could not open file!");
  158. }
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. }
  167. }
m
0
l
March 6, 2011 2:13:52 PM

Scratch that about not getting the PrintStream to work, I forgot to add the import for PrintStream. It is working great now. Just need to fix the 99 not quitting right away. I know why it is not quitting right away, because once the validation part at line 98 is checked, even if its 99 it will always run the "Pick 2 numbers." Not sure where to put it then, other then making the switch statements bulkier.


Also how can I make the file created to my desktop, not to where ever the program default is?
m
0
l
a b L Programming
March 6, 2011 2:32:30 PM

Don't forget the out.close() also at the end.

As for the "not quitting right away", just put the 2 number input in an "if(choice != 99)" block.
m
0
l
March 6, 2011 2:55:48 PM

Okay everything is working, thanks guys, just some little questions. In my div method, I am validating so that you can't divided by 0. Here is the code:

  1. public int div(int num1, int num2){
  2.  
  3. int sum = 0;
  4. if (num2 == 0)
  5. {
  6. System.out.println("ERROR: Can't divide by 0");
  7.  
  8. }
  9. else
  10. {
  11. sum = num1 / num2;
  12.  
  13. }
  14.  
  15. return sum;
  16. }


When you do it you get the error message but you also get the "The sum is 0" since I have to initialize sum as 0. How can I get it to just leave the method after the error message is shown and skip the return sum statement?
m
0
l
a b L Programming
March 6, 2011 3:07:06 PM

The only way to leave a method without having to return anything is to throw an exception.

Alternatively, one could also return a predefined "error" value (called a sentinel), but it's not always possible; for your operation for example, any value could be valid so no sentinel possible. A way around this specific problem for you, would be to use the "wrapper" class Integer, by having an object as a return value, you could also return "null" as a value which could be your sentinel to indicate something was wrong.
m
0
l
a b L Programming
March 6, 2011 3:14:16 PM

Oh, just a side-note that the use of sentinels is sometimes frown upon in Object Oriented Programming, some say this is just old procedural programming practices. Personally I don't care that much as long as it's clear and documented.
m
0
l
March 6, 2011 3:28:08 PM

lol that a whole bunch of things not learned yet lol. So pretty much the way I have it there is no way around it? Unless I look up what sentinels are. I tried to make sum = null but got errors, is it because null only works with strings?
m
0
l
a b L Programming
March 6, 2011 4:29:04 PM

laserpp said:
lol that a whole bunch of things not learned yet lol. So pretty much the way I have it there is no way around it? Unless I look up what sentinels are. I tried to make sum = null but got errors, is it because null only works with strings?
null only works on objects and all basic types (int, long, char, float, double, ...) are what is called primitives; they do not support null. You could always make the check before calling calc.div().
m
0
l
March 6, 2011 4:36:23 PM

Zenthar said:
null only works on objects and all basic types (int, long, char, float, double, ...) are what is called primitives; they do not support null. You could always make the check before calling calc.div().


I changed it to this. Works way better. thanks
m
0
l
March 6, 2011 4:39:30 PM

Best answer selected by laserpp.
m
0
l
!