Solved

Calculator program - JAVA

18 answers Last reply Best Answer
More about calculator program java
  1. 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
  2. I added the out.close but still the file is opening up blank. =/ I updated the code above.
  3. 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()".
  4. Best answer
    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.
  5. good catch Zenthar on the opening the file again
  6. 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.
  7. 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
  8. 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.
  9. 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?
  10. 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.
  11. 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:

    
    public int div(int num1, int num2){
    	
    		int sum = 0;
    		if (num2 == 0)
    		{
    			System.out.println("ERROR: Can't divide by 0");
    			
    		}
    		else
    		{
    			sum = num1 / num2;
    			
    		}
    		
    		return sum;
    	}
    


    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?
  12. 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.
  13. 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.
  14. 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?
  15. 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().
  16. 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
  17. Best answer selected by laserpp.
Ask a new question

Read More

Programming Java Apps