2012-09-27 62 views
0

我在解決CCE時遇到問題。此分配要求用戶在運行時打開.txt文件,然後讓編譯器 對特定正則表達式執行後臺解析,以確定它們是否匹配或不匹配(例如,ACC或REJ),但捕獲是該用戶必須用JFileChooser打開該文件。我已經做了大量的閱讀和 研究發佈之前,問題似乎是 JFileChooser,據我瞭解使用java.io.File本身,似乎 只允許掃描儀解析輸入的.txt文件在此場景。JFileChooser和.txt文件解析問題

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

所以,當我單獨使用掃描器讀取文本的所有行的文件 在它的編譯「,但所有的控制檯輸出表現爲,例如,「空 空空空空」 (例如,如果文件中有五行文字)。 爲了方便起見,下面是.txt文件的一個例子,將被認爲是可接受的 (或ACC,速記):EDIT這個例子中應具有的每個印刷在單獨的行

S中的規則:AB 甲:0A 答:E B:1B B:è

我提供我的源代碼。請注意,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 

底線是,無論在源代碼中也沒有我的掃描儀和BufferedReader對象的類型轉換的位置,我總是得到一個CCE。任何幫助將是巨大的。先謝謝你。

+0

你可以在這裏粘貼你的堆棧跟蹤 – Michael

+0

@ Michael hello there。堆棧跟蹤讀取爲:Thread [main](暫停(異常ClassCastException))Simplified.readLines()行:83簡化。 ()line:21 Simplified.main(String [])line:179 – OcelotXL

回答

1
  1. JFileChooser.getSelectedFile返回File,我不知道爲什麼你想將其包裝在一個Comparable<File>的?文件本身實現了Comparable
  2. Scanner將接受File作爲輸入。儘管我對Scanner API沒有經驗,但我認爲您不需要BufferedReader,只需創建Scanner並使用File即可,因爲它的源代碼應該足以讓您運行。

他是一個快速測試,你可以嘗試。

  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()); 
} 

從那裏開始添加在您的分析邏輯。

+0

好酷的豆子我今天晚些時候會試試這個thx man – OcelotXL