I’m having trouble resolving a CCE. This assignment calls for
the user to open a .txt file at runtime and then have the compiler
do background parsing on particular regexes to determine if they
match or don’t match (e.g., ACC or REJ), but the catch is that
the user MUST open the file with a JFileChooser. I’ve done much reading and
research before posting this and the problem seems to be that
JFileChooser, which as I understand uses java.io.File itself, seems
to only allow Scanner to parse input of a .txt file in this scenario.
http://docs.oracle.com/javase/1.5.0/docs/api/java/io/class-use/File.html
So, when I used solely Scanner to read all the lines of text in the file
it ‘compiled’ but all the console output showed was, for example, “null
null null null null” (e.g., if there were five lines of text in the file).
For convenience, the following is an example of .txt file that would be considered
acceptable (or ACC, for shorthand): EDIT this example should have each of the rules printed on a separate line
S:AB
A:0A
A:e
B:1B
B:e
I have provided my source code. Note that charSequences are just the viable
regexes the Scanner/BR has to parse. Also, if any given line is rejecting (REJ,
for shorthand), we need to to alert the user then terminate immediately.Apparently
I don’t understand these classes as well as I thought I did…maybe I need to
use something like FileReader and I’m way off base to start with but I’m just
extremely frustrated with the complication of java IO in general. It’s just so discouraging
because I put all this work and effort in and rarely have anything to show for it.
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
Bottom line is that no matter the placement in the source code nor the type casting of my Scanner and BufferedReader objects, I’m always getting a CCE. Any help would be tremendous. Thank you in advance.
JFileChooser.getSelectedFilereturns aFile, I don’t know why you’re trying to wrap it in aComparable<File>for?? File itself implementsComparable.Scannerwill accept aFileas input. While I’m not experienced with theScannerAPI, I don’t think you need theBufferedReader, simply creating theScannerwith aFileas it source should be enough to get you running.He’s a quick test you can try.
Classthat has just apublic static void main(String args[]) {...}method.In the
mainmethod try something like…From there start adding in your parsing logic.