Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从方法中打印到文件_Java_Printing_Expression_Bufferedwriter_Infix Notation - Fatal编程技术网

Java 从方法中打印到文件

Java 从方法中打印到文件,java,printing,expression,bufferedwriter,infix-notation,Java,Printing,Expression,Bufferedwriter,Infix Notation,我正在获取一个包含各种中缀表达式的输入文件,计算它们,然后将它们打印回另一个输出文件,每行的格式如下: %%的模10值为% 输出文本和模10答案均正确;但是,我无法让程序重新打印介于“OF”和“IS”之间的整个表达式 我尝试将output.write(token)放在getToken()方法中,但出现了“找不到符号”错误。所以我知道我不能从另一个方法访问BufferedWriter,因为它是在main中声明的,但是我如何才能绕过它呢 import java.io.*; public class

我正在获取一个包含各种中缀表达式的输入文件,计算它们,然后将它们打印回另一个输出文件,每行的格式如下:

%%的模10值为%

输出文本和模10答案均正确;但是,我无法让程序重新打印介于“OF”和“IS”之间的整个表达式

我尝试将
output.write(token)
放在getToken()方法中,但出现了“找不到符号”错误。所以我知道我不能从另一个方法访问BufferedWriter,因为它是在main中声明的,但是我如何才能绕过它呢

import java.io.*;

public class Lab1
{
public static char token;
public static String expr;
public static int k = 0;

public static void main (String[] args)
{
    int exprValue;
    String line;

    try
    {
        BufferedReader input = new BufferedReader(new FileReader("inputfile.txt"));
        BufferedWriter output = new BufferedWriter(new FileWriter("outputfile.txt"));

        while ((line = input.readLine()) != null)
        {               
            output.write("THE MODULO 10 VALUE OF ");
            expr = line;
            getToken();             
            output.write(token);
            exprValue = expression();
            output.write(" IS " + exprValue);
            output.newLine();               
            output.newLine();               
            k = 0;
        }

        input.close();
        output.close();

    }

    catch (IOException ex)
    {
        System.err.println("Exception:" + ex);
    }

}

public static void getToken()
{
    k++;

    int count = k-1;

    if(count < expr.length())
    {
        token = expr.charAt(count);
    }
}

public static int expression()
{
    int termValue;
    int exprValue;

    exprValue = term();

    while(token == '+')
    {
        getToken();
        termValue = term();
        exprValue = (exprValue + termValue)%10;
    }

    return exprValue;
}

public static int factor()
{
    int factorValue = token;

    if(Character.isDigit(token))
    {
        factorValue = Character.getNumericValue(token);
        getToken();
    }
    else if(token == '(')
    {
        getToken();
        factorValue = expression();

        if(token == ')')
        {
            getToken();
        }
    }

    return factorValue;
}

public static int term()
{
    int factorValue;
    int termValue;

    termValue = factor();
    while(token == '*')
    {
        getToken();
        factorValue = factor();
        termValue = (termValue * factorValue)%10;
    }

    return termValue;
}
}
import java.io.*;
公共类Lab1
{
公共静态字符令牌;
公共静态字符串表达式;
公共静态int k=0;
公共静态void main(字符串[]args)
{
int exprValue;
弦线;
尝试
{
BufferedReader输入=新的BufferedReader(新文件读取器(“inputfile.txt”);
BufferedWriter输出=新的BufferedWriter(新的FileWriter(“outputfile.txt”);
而((line=input.readLine())!=null)
{               
输出。写入(“的模10值”);
expr=直线;
getToken();
输出。写入(令牌);
exprValue=表达式();
output.write(“IS”+exprValue);
output.newLine();
output.newLine();
k=0;
}
input.close();
output.close();
}
捕获(IOEX异常)
{
System.err.println(“异常:+ex”);
}
}
公共静态void getToken()
{
k++;
整数计数=k-1;
如果(计数
目前我的输入是:

(3*6+4)*(4+5*7)

3*((4+5*(1+6)+2))

我的输出是:

(的模10值为8


3的模10值为3解决了这个问题。在主方法的while循环中,将
output.write(token)
替换为
output.write(expr)

经过进一步的研究,我相当肯定将
output.write(token)
放在getToken()中方法将解决我的问题,但我该如何做?解决了问题。在主方法的while循环中,将
output.write(token)
替换为
output.write(expr)