How to perform operations within a file and write the result to another output file?

0

Good morning, I am having some problems in a Java exercise that I had in my class. What I need to do is in a .txt input file put any content either words or numbers but at the time of having an operation, the result should be shown in an output .txt file along with the text you have written.

At the moment I was able to realize that the contents of the input file will be reflected in the output file, but I have problems in the operations part.

I have this code where I try to carry out the operations but it marks me an error when entering the names, this is:

C: \ Users \ PC \ Desktop> java miCalc enter.txt output.txt     Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0         at miCalc. (miCalc.java:35)         at miCalc.main (miCalc.java:11)

import java.io.*;
public class miCalc{
    String input, output;
    char operacion;
    String[] num;
    float resultado;
    String dataFiles;

    public static void main(String args[]){
        String input = args[0];
        String output = args[1];
        miCalc fileOp = new miCalc(input,output);
    }
    public miCalc(String input, String output){
        this.input = input;
        this.output = output;
        try{
            File f1 = new File(input);
            FileInputStream fstream = new FileInputStream(f1);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter fw = new FileWriter(output,true);
            BufferedWriter bw = new BufferedWriter(fw);
            do{
                dataFiles=br.readLine();
                if(dataFiles!=null){
                    for(int i=0;i<dataFiles.length();i++){
                        if(dataFiles.charAt(i)=='+' || dataFiles.charAt(i)=='-' || dataFiles.charAt(i)=='*' || dataFiles.charAt(i)=='/'){
                            operacion=dataFiles.charAt(i);
                        }
                    }
                    String c=operacion+"";
                    num=dataFiles.split("\"+c);
                    if(operacion=='+'){
                         resultado=Float.parseFloat(num[0])+Float.parseFloat(num[1]);
                        bw.write(resultado+"");
                        bw.newLine();
                    }

                    else if(operacion=='-'){
                        resultado=Float.parseFloat(num[0])-Float.parseFloat(num[1]);
                        bw.write(resultado+"");
                        bw.newLine();
                    }
                    else if(operacion=='*'){
                        resultado=Float.parseFloat(num[0])*Float.parseFloat(num[1]);
                        bw.write(resultado+"");
                        bw.newLine();
                    }
                    else if(operacion=='/'){
                        resultado=Float.parseFloat(num[0])/Float.parseFloat(num[1]);
                        bw.write(resultado+"");
                        bw.newLine();
                    }

                }
            }while(dataFiles!=null);
                br.close();
                bw.close();
        }
        catch(FileNotFoundException e){}
        catch(IOException e){}
    }
}

Any idea what might be causing the error?

Thank you.

    
asked by Space_Shift 13.03.2017 в 00:45
source

1 answer

1

You have to clean the variable operacion , you can define after the declaration of do . Currently it remains stored with the value obtained from the last operation and that is why it causes error when it reads the following line.

Example:

...
BufferedWriter bw = new BufferedWriter(fw);
do {
    operacion = '
import java.io.*;
import java.util.regex.Pattern;

public class miCalc {
    String input, output;
    char operacion;
    String[] num;
    float resultado;
    String dataFiles;
    String REGEX = "^[0-9\-\+\*\/\.]*"; // --------------------------------------------> valida que haya numeros los signos de operación y caracter decimal

    public static void main(String args[]) {
        String input = args[0];
        String output = args[1];
        miCalc fileOp = new miCalc(input, output);
    }

    public miCalc(String input, String output) {
        this.input = input;
        this.output = output;
        try {
            File f1 = new File(input);
            FileInputStream fstream = new FileInputStream(f1);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter fw = new FileWriter(output, true);
            BufferedWriter bw = new BufferedWriter(fw);
            do {
                operacion = '
...
BufferedWriter bw = new BufferedWriter(fw);
do {
    operacion = '
import java.io.*;
import java.util.regex.Pattern;

public class miCalc {
    String input, output;
    char operacion;
    String[] num;
    float resultado;
    String dataFiles;
    String REGEX = "^[0-9\-\+\*\/\.]*"; // --------------------------------------------> valida que haya numeros los signos de operación y caracter decimal

    public static void main(String args[]) {
        String input = args[0];
        String output = args[1];
        miCalc fileOp = new miCalc(input, output);
    }

    public miCalc(String input, String output) {
        this.input = input;
        this.output = output;
        try {
            File f1 = new File(input);
            FileInputStream fstream = new FileInputStream(f1);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            FileWriter fw = new FileWriter(output, true);
            BufferedWriter bw = new BufferedWriter(fw);
            do {
                operacion = '%pre%';
                dataFiles = br.readLine();
                if (dataFiles != null) {
                    for (int i = 0; i < dataFiles.length(); i++) {
                        if (dataFiles.charAt(i) == '+' || dataFiles.charAt(i) == '-' || dataFiles.charAt(i) == '*' || dataFiles.charAt(i) == '/') {
                            operacion = dataFiles.charAt(i);
                        }
                    }
                    String c = operacion + "";
                    Pattern.compile(REGEX); // ---------------------------------------------> Compila la expresión regular

                    if (dataFiles.matches(REGEX) && operacion != '%pre%') { // ----------------> Evalua que se cumpla con la expresión regular
                        num = dataFiles.split("\" + c);
                        if (operacion == '+') {
                            resultado = Float.parseFloat(num[0]) + Float.parseFloat(num[1]);
                            bw.write(resultado + "");
                            bw.newLine();
                        } else if (operacion == '-') {
                            resultado = Float.parseFloat(num[0]) - Float.parseFloat(num[1]);
                            bw.write(resultado + "");
                            bw.newLine();
                        } else if (operacion == '*') {
                            resultado = Float.parseFloat(num[0]) * Float.parseFloat(num[1]);
                            bw.write(resultado + "");
                            bw.newLine();
                        } else if (operacion == '/') {
                            resultado = Float.parseFloat(num[0]) / Float.parseFloat(num[1]);
                            bw.write(resultado + "");
                            bw.newLine();
                        }
                    } else {
                        bw.write(dataFiles);
                        bw.newLine();
                    }
                }
            } while (dataFiles != null);
            br.close();
            bw.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }
}
'; //-------------------> Aquí limpias la variable dataFiles = br.readLine(); if (dataFiles != null) { ....
'; dataFiles = br.readLine(); if (dataFiles != null) { for (int i = 0; i < dataFiles.length(); i++) { if (dataFiles.charAt(i) == '+' || dataFiles.charAt(i) == '-' || dataFiles.charAt(i) == '*' || dataFiles.charAt(i) == '/') { operacion = dataFiles.charAt(i); } } String c = operacion + ""; Pattern.compile(REGEX); // ---------------------------------------------> Compila la expresión regular if (dataFiles.matches(REGEX) && operacion != '%pre%') { // ----------------> Evalua que se cumpla con la expresión regular num = dataFiles.split("\" + c); if (operacion == '+') { resultado = Float.parseFloat(num[0]) + Float.parseFloat(num[1]); bw.write(resultado + ""); bw.newLine(); } else if (operacion == '-') { resultado = Float.parseFloat(num[0]) - Float.parseFloat(num[1]); bw.write(resultado + ""); bw.newLine(); } else if (operacion == '*') { resultado = Float.parseFloat(num[0]) * Float.parseFloat(num[1]); bw.write(resultado + ""); bw.newLine(); } else if (operacion == '/') { resultado = Float.parseFloat(num[0]) / Float.parseFloat(num[1]); bw.write(resultado + ""); bw.newLine(); } } else { bw.write(dataFiles); bw.newLine(); } } } while (dataFiles != null); br.close(); bw.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } } }
'; //-------------------> Aquí limpias la variable dataFiles = br.readLine(); if (dataFiles != null) { ....

It is necessary to evaluate that the line read does not have numbers and letters because an execution error would occur, this can be corrected using regular expressions, here the complete example:

%pre%     
answered by 14.03.2017 / 03:44
source