我在解决CCE时遇到问题。这项任务要求 用户在运行时打开.txt文件然后使用编译器 对特定正则表达式进行背景解析以确定它们是否正确 匹配或不匹配(例如,ACC或REJ),但问题是 用户必须使用JFileChooser打开文件。我做了很多阅读 发布之前的研究和问题似乎就是这样 JFileChooser,据我所知使用java.io.File本身,似乎 在此方案中仅允许扫描程序解析.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,简写):编辑此示例应将每个规则打印在单独的行上
S:AB 答:0A 答:E B:1B B:ë
我提供了我的源代码。请注意,charSequences是可行的 扫描程序/ 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。任何帮助都将是巨大的。先感谢您。
答案 0 :(得分:1)
JFileChooser.getSelectedFile
返回File
,我不知道为什么你要把它包裹在Comparable<File>
中?文件本身实现了Comparable
。Scanner
将接受File
作为输入。虽然我对Scanner
API没有经验,但我认为您不需要BufferedReader
,只需使用Scanner
创建File
,因为它的来源应该足够让你跑步。他是一个你可以尝试的快速测试。
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());
}
从那里开始添加你的解析逻辑。