Zhihao ZHAO
Zhihao ZHAO

Reputation: 35

java, incorrect output in the form of txt file (simple two inputed values from the console and output the value of addition)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws FileNotFoundException{
        int option = 0;
        int a;
        int b;

        System.out.println("Input the type of the calculation:");

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(new File("C:\\Users\\123\\Desktop\\result.txt"));

        option = in.nextInt();

        System.out.println("Input the two values:");
        System.out.print("a:");
        a = in.nextInt();

        System.out.print("b:");
        b = in.nextInt();

        in.close();

        // the Calculation:
        switch(option) {
        case 1:
            out.write(Integer.toString(a+b));
            System.out.println(a + b);
        case 2:
            out.write(Integer.toString(a - b));
        case 3:
            out.write(Integer.toString(a * b));
        case 4:
            out.write(Double.toString(1.0 * a / b));
        }

        out.flush();
        out.close();
    }
}

This is the code, I am using the value of a=12, b=4 as test example, and I input 1 for option(which makes the program doing addition from the selection of switch ), the result of System.out.print is right which is 16, but the result outputed using PrintWriter is not correct, not only the value, but also the value type which is float(or double, but it should be int), with a value of 168483.0, I am really new to java and cannot figure out this problem.

Upvotes: 0

Views: 74

Answers (1)

Stephen P
Stephen P

Reputation: 14800

When I run this, I get the expected output in the result.txt file, but that is followed by other numbers.

This is happening because you don't have a break statement in each case, so the sum (case #1) is calculated, but then the code falls through to subtraction (case #2), then multiplication and division, outputting each result to the file with no delimiters or newlines to separate them.

Your output is 168483.0 -- this is:
• 12 + 4 = 16
• 12 - 4 = 8
• 12 * 4 = 48
• 12 / 4 = 3.0
With spacing it would look like: 16 8 48 3.0

It's also good practice to include a default: case, for example what if someone enters 9 for the type of the calculation.

This would make your switch statement should look like this:

switch(option) {
    case 1:
        out.write(Integer.toString(a+b));
        System.out.println(a + b);
        break;            // <---- added a "break" for each case
    case 2:
        out.write(Integer.toString(a - b));
        break;
    case 3:
        out.write(Integer.toString(a * b));
        break;
    case 4:
        out.write(Double.toString(1.0 * a / b));
        break;
    default:
        System.out.println("Operations are: 1=add 2=subtract 3=multiply 4=divide");
        break;
}

Upvotes: 3

Related Questions