0

我无法解决 CCE。此分配要求用户在运行时打开一个 .txt 文件,然后让编译器对特定的正则表达式进行后台解析以确定它们是否匹配(例如,ACC 或 REJ),但问题是用户必须使用 JFileChooser 打开文件。在发布之前我已经做了很多阅读和研究,问题似乎是 JFileChooser,据我所知,它使用 java.io.File 本身,似乎只允许 Scanner 在这种情况下解析 .txt 文件的输入。

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/class-use/File.html

因此,当我仅使用 Scanner 读取文件中的所有文本行时,它“编译”但所有控制台输出显示为“null null null null null”(例如,如果有五行文本文件)。为方便起见,以下是一个可以接受的 .txt 文件示例(或 ACC,简称 ACC):编辑此示例应将每个规则打印在单独的行上

S:AB A:0A A:e B:1B B:e

我已经提供了我的源代码。请注意,charSequences 只是 Scanner/BR 必须解析的可行正则表达式。此外,如果任何给定的行被拒绝(REJ,简写),我们需要提醒用户然后立即终止。显然我并不像我想的那样理解这些类......也许我需要使用一些东西像 FileReader 一样,我一开始就离基地很远,但我对 Java IO 的一般复杂性感到非常沮丧。这太令人沮丧了,因为我投入了所有这些工作和努力,却很少有任何东西可以展示。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException; 
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Simplified 
{


public Simplified() throws Exception
{   
    readLines();
}

public void readLines() throws Exception
{

    //MUST prompt user to use JFileChooser
    JFileChooser ranFi = new JFileChooser();
    //approve condition
    if(ranFi.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
    {
        //get selected file 
        Comparable<File> file = ranFi.getSelectedFile();
        //create a bufferedReader for the file 
        //BufferedReader br = new BufferedReader((Reader) file);
        BufferedReader br = null;

        //create local String object the refer to file's current line 
        String currentLine;

        /**************************
         * Length(LHS,RHS) = 1,1
         * --CharSequence compilations
         * ******************************
         */
        //ACC
        CharSequence fp100 = "[S][:][A-Ze0-1]";
        //ACC iff is NOT the very first line of text file 
        CharSequence fp101 = "[A-Z&&[^S]][:][A-Ze0-1]";


        /**************************
         * Length(LHS,RHS) = 1,2
         * --CharSequence compilations
         * ******************************
         */
        //ACC 
        CharSequence fp200 = "[S][:][A-Z0-1][A-Z0-1]";
        //ACC iff is NOT the very first line of text file 
        CharSequence fp201 = "[A-Z&&[^S]][:][A-Z0-1][A-Z0-1]";

        /**************************
         * Length(LHS,RHS) = 1,3 
         * --CharSequence compilations
         * ******************************
         */
        //ACC 
        CharSequence fp300 = "[S][:][A-Z0-1][A-Z0-1][A-Z0-1]";
        //ACC iff is NOT the very first line of text file 
        CharSequence fp301 = "[A-Z&&[^S]][:][A-Z0-1][A-Z0-1][A-Z0-1]";

        /**
         * Length(LHS,RHS) = 1,4 
         * --CharSequence compilations
         * ******************************
         */

        //REJ
        CharSequence fp400 = "[.][.][.][.][.][.]";


        //create a Scanner for the file 
        Scanner text = new Scanner((Readable) file);

        try
        {
            br = new BufferedReader((Reader) file);
            //while there are still lines to be read in the file 
            //where currentLine = present line of the bufferedReader
            while((currentLine = br.readLine()) != null)
            {

                //while the scanner still has lines to read in the file 
                while(text.hasNext())
                {


                //to remove trailing whitespace in the file, starting
                //@ the first line in the text file...
                String trimStartLine = currentLine.trim();

                //ALL feasible ACC permutations for starting line 
                if(trimStartLine.contains(fp100)||trimStartLine.contains(fp200)||trimStartLine.contains(fp300))
                {
                    System.out.println("first line valid...");


                }
                //ALL feasible REJ permutations for starting line
                else if(!trimStartLine.contains(fp100)||!trimStartLine.contains(fp200)||!trimStartLine.contains(fp300))
                {
                    System.out.println("invalid first line..." +
                            "...terminating");
                    System.exit(0);
                }

                //once again removing trailing whitespace in the file,
                //this time trimming the whitespace in the second line
                //of the file, provided that iff the first line of the
                //file was valid to begin with...
                String trim2ndLine = currentLine.trim();

                //permutations have now increased to 6 since "[S]" isn't
                //technically required for ANY line other than the first line 
                if(trim2ndLine.contains(fp100)||trim2ndLine.contains(fp101)||trim2ndLine.contains(fp200)||
                        trim2ndLine.contains(fp201)||trim2ndLine.contains(fp300)||trim2ndLine.contains(fp301))
                {
                    System.out.println("2nd line valid...");

                    //only included two line checks for now...
                    //as of now if the first two lines are valid
                    //then print out the remaining lines in the
                    //file...
                    System.out.println(text.nextLine());

                }
                else if(!trim2ndLine.contains(fp100)||!trim2ndLine.contains(fp101)||!trim2ndLine.contains(fp200)||
                        !trim2ndLine.contains(fp201)||!trim2ndLine.contains(fp300)||!trim2ndLine.contains(fp301))
                {
                    System.out.println("invalid 2nd line..." +
                            "...terminating");
                    System.exit(0);
                }
                else
                {
                    System.out.println("no file was selected");
                }

            }//end of inner while
            }//end of outer while 
        }//end of try
                catch (Exception ex)
                {
                    System.out.println(ex.getMessage());
                }   
                finally
                {   
                    try
                    {   //close text while lines remain,
                        //valid or not...
                        if(br != null && text != null){
                            br.close();
                            text.close();
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }//end of finally 
        System.out.println("end of file successfully reached...");
    }//end of approve option
}//end of method 




public static void main(String[] args) throws    Exception
{
    Simplified mn = new Simplified();
    mn.readLines();
}//end of main

}//end of Simplified.java 

底线是,无论源代码中的位置或我的 Scanner 和 BufferedReader 对象的类型转换,我总是得到一个 CCE。任何帮助都将是巨大的。先感谢您。

4

1 回答 1

1
  1. JFileChooser.getSelectedFile返回 a File,我不知道您为什么要尝试将其包装在 a Comparable<File>for?? 文件本身实现Comparable.
  2. Scanner将接受 aFile作为输入。虽然我对ScannerAPI 没有经验,但我认为您不需要BufferedReader,只需Scanner使用 aFile作为它的源来创建就足以让您运行。

他是一个快速测试,你可以试试。

  1. 为您自己创建一个包含您需要测试的内容的简单文本文件。
  2. 创建一个Class只有一个public static void main(String args[]) {...}方法的简单对象。

main方法中尝试类似...

Scanner scanner = new Scanner(new File("path/to/text/file/text.txt"));
while (scanner.hasNext()) {
    System.out.println(scanner.next());
}

从那里开始添加您的解析逻辑。

于 2012-09-27T22:57:40.887 回答