Home > Blockchain >  How to append every true if, else if statement to a text file?
How to append every true if, else if statement to a text file?

Time:02-25

How do I append every user chosen equation in an if, else if statement to a text file. I am not sure where would the best place be to put the append to file because putting it in under every else if statement seems repetitive.

import java.util.Formatter;
import java.util.Scanner;

public class javaCalculator {
    public static void main(String[] args){
        
        Scanner numbers = new Scanner(System.in);
        Scanner operation = new Scanner(System.in);

        double number1;
        double number2;
        String operator;

       
        System.out.print("Enter the operator you would like to choose( , -, *, /): ");
        operator = operation.next();

        
        System.out.print("Enter the first number: ");
        number1 = Double.parseDouble(numbers.nextLine());

        System.out.print("Enter your second number: ");
        number2 = Double.parseDouble(numbers.nextLine());


        
        if (operator.equals(" ")){
            String calculation = (number1   "   "   number2   " = "   (number1   number2));

        }else if (operator.equals("-")){
            String calculation = (number1   " - "   number2   " = "   (number1 - number2));

        }else if (operator.equals("*")){
            String calculation = (number1   " * "   number2   " = "   (number1 * number2));

        }else if (operator.equals("/")){
            String calculation = (number1   " / "   number2   " = "   (number1 / number2));

        }else{
            String calculation = (operator   ":"   " Is not a valid operator!");

        }
        try {
            Formatter file1 = new Formatter("C:\\Users\\27711\\Desktop\\PROGRAMMING\\java\\JavaCalculator");
            file1.format(calculation);

        }
        catch (Exception e){
            System.out.println("Error");
        }
    }
}

CodePudding user response:

There are two problems here:

  • Attempting to use a variable when it isn't in scope
  • The way you're writing to a file using Formatter

Making the calculation variable available

All you need to do is declare your calculation variable once, before the if/else statements:

String calculation;

if (operator.equals(" ")) {
    calculation = number1   "   "   number2   " = "   (number1   number2);
} else if (operator.equals("-")) {
    calculation = number1   " - "   number2   " = "   (number1 - number2);
} else if (operator.equals("*")) {
    String calculation = number1   " * "   number2   " = "   (number1 * number2);
} else if (operator.equals("/")) {
    calculation = number1   " / "   number2   " = "   (number1 / number2);
} else {
    calculation = operator   ":"   " Is not a valid operator!";
}
// Code to append to the file

In your current code, you're declaring a different variable in each if/else block, which means no calculation variable is in scope by the time you reach the file appending code.

An alternative approach would be to use a conditional operator. Some folks really don't like multiple uses like this, but I personally think it ends up being very readable:

String calculation =
    operator.equals(" ") ? number1   "   "   number2   " = "   (number1   number2)
  : operator.equals("-") ? number1   " - "   number2   " = "   (number1 - number2)
  : operator.equals("*") ? number1   " * "   number2   " = "   (number1 * number2)
  : operator.equals("/") ? number1   " / "   number2   " = "   (number1 / number2)
  : operator   ":"   " Is not a valid operator!";

Alternatively, you might want to consider a switch statement, maybe in a whole separate method:

private static String calculate(String operator, double number1, double number2) {
    switch (operator) {
        case " ":
            return number1   "   "   number2   " = "   (number1   number2);
        case "-":
            return number1   " - "   number2   " = "   (number1 - number2);
        case "*":
            return number1   " * "   number2   " = "   (number1 * number2);
        case "/":
            return number1   " / "   number2   " = "   (number1 / number2);
        default:
            return operator   ":"   " Is not a valid operator!"
    }
}

... or a switch expression if you're using a recent version of the language.

Writing to the file

The documentation for Formatter starts:

An interpreter for printf-style format strings.

While you can use a Formatter to write to a file, you don't want to interpret printf-style format strings at all... and you're currently not closing the formatter, either.

I'd suggest using the Files class instead. You can just write:

Files.write(Paths.get(filename), Arrays.asList(calculation));

To append, you can pass in StandardOpenOptions.APPEND:

Files.write(
    // Where to write...
    Paths.get(filename),
    // The lines to write...
    Arrays.asList(calculation),
    // How to open the file...
    StandardOpenOption.APPEND);
  •  Tags:  
  • java
  • Related