从文件读入时如何使用字符串标记器? [英] How to use string tokenizer when reading in from a file?

查看:120
本文介绍了从文件读入时如何使用字符串标记器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java实现一个RPN计算器,并且需要帮助创建一个类来将方程解析为单独的标记。

I am implementing a RPN calculator in Java and need help creating a class to parse the equations into separate tokens.

我的输入文件将具有未知数量的方程类似于下面显示的那些:

My input file will have an unknown number of equations similar to the ones shown below:


49+62*61-36
4/64
(53+26)
0*72
21-85+75-85
90*76-50+67
46*89-15
34/83-38
20/76/14+92-15

我已经实现了自己的通用堆栈类,可以在程序中使用,但我现在正试图弄清楚如何从输入文件中读取数据。任何帮助表示赞赏。

I have already implemented my own generic stack class to be used in the program, but I am now trying to figure out how to read data from the input file. Any help appreciated.

我已经在 PasteBin ,万一它可能会有所帮助。

I've posted the source code for my stack class at PasteBin, in case it may help.

我还将没有文件读取的计算器上传到 PasteBin 显示我已经完成的工作。

I have also uploaded the Calculator with no filereading to PasteBin to show what I have done already.

我现在设法将文件读入并且因为帮助,令牌被打破了。当它到达文件末尾时我收到错误并且想知道如何解决这个问题?

I have now managed to get the file read in and the tokens broken up thanks for the help. I am getting an error when it reaches the end of the file and was wondering how to solve that?

这是代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

 public class TestClass {    
  static public void main(String[] args) throws IOException {
 File file = new File("testEquations.txt");
  String[] lines = new String[10];
  try {
    FileReader reader = new FileReader(file);
    BufferedReader buffReader = new BufferedReader(reader);
    int x = 0;
    String s;
    while((s = buffReader.readLine()) != null){
        lines[x] = s;
        x++;
    }
 }
  catch(IOException e){
    System.exit(0);
}
String OPERATORS = "+-*/()";

for (String st : lines) {
    StringTokenizer tokens = new StringTokenizer(st, OPERATORS, true);
    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken();
        if (OPERATORS.contains(token))
            handleOperator(token);
        else
            handleNumber(token);
    }
     }
   }

private static void handleNumber(String token) {
System.out.println(""+token);

   }

 private static void handleOperator(String token) {
System.out.println(""+token);

  }
  }

另外,我如何确保RPN逐行工作?我对我想要遵循的算法感到非常困惑。

Also How would I make sure the RPN works line by line? I am getting quite confused by the algorithms I am trying to follow.

推荐答案

因为你的问题现在完全改变了它的原始版本 - 这是对原始文件的回应,即如何使用FileReader从文件中获取值。

As your question has now changed completely from it's original version - this is in response to your original one, which was how to use FileReader to get the values from your file.

这会将每行放入一个单独的元素中字符串数组。您应该使用ArrayList,因为它更灵活,但我刚刚做了一个快速演示 - 您可以按照自己的意愿清理它,虽然我注意到您使用的代码需要一个String数组作为它的输入。也许您最初可以将值读入ArrayList,然后在拥有所有行后将其复制到数组中 - 这样您可以根据需要添加任意数量的行,并使代码保持灵活,以便更改您的行数输入文件。

This will put each line into a separate element of a String array. You should probably use an ArrayList instead, as it's far more flexible, but I have just done this as a quick demo - you can clean it up as you wish, although I notice the code you are using expects a String array as it's input. Perhaps you could read the values initially into an ArrayList, then copy that to an array once you have all the lines - that way you can put as many lines in as you wish and keep your code flexible for changes in the number of lines in your input file.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class TestClass {    
  static public void main(String[] args) {
    File file = new File("myfile.txt");
    String[] lines = new String[10];
    try {
        FileReader reader = new FileReader(file);
        BufferedReader buffReader = new BufferedReader(reader);
        int x = 0;
        String s;
        while((s = buffReader.readLine()) != null){
            lines[x] = s;
            x++;
        }
    }
    catch(IOException e){
        //handle exception
    }
    // And just to prove we have the lines right where we want them..
    for(String st: lines)
    System.out.println(st);
  }
}

您之前提到过您使用的代码链接:

You mentioned before that you were using the code on this link:

http://www.technical-recipes.com/2011/a-mathematical-expression-parser-in-java/#more-1658

这看来已经处理过运营商优先权了吗?从数组中解析每个String并将它们分类为数字或运算符?从我的快速看,它至少似乎这样做。

This appears to already deal with operator precedence doesn't it? And with parsing each String from the array and sorting them into numbers or operators? From my quick look it at least it appears to do that.

所以看起来你需要的只是你的行在一个String数组中,然后你传递给你已经拥有的代码。无论如何,从我所看到的情况来看。

So it looks like all you need is for your lines to be in a String array, which you then pass to the code you already have. From what I can see anyway.

显然这并没有解决大于9的数字问题,但希望它对上半年有帮助。

Obviously this doesn't address the issue of numbers greater than 9, but hopefully it helps with the first half.

: - )

这篇关于从文件读入时如何使用字符串标记器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆