Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8592585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:47:09+00:00 2026-06-11T23:47:09+00:00

I’m having trouble resolving a CCE. This assignment calls for the user to open

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:47:10+00:00Added an answer on June 11, 2026 at 11:47 pm
    1. JFileChooser.getSelectedFile returns a File, I don’t know why you’re trying to wrap it in a Comparable<File> for?? File itself implements Comparable.
    2. Scanner will accept a File as input. While I’m not experienced with the Scanner API, I don’t think you need the BufferedReader, simply creating the Scanner with a File as it source should be enough to get you running.

    He’s a quick test you can try.

    1. Create your self a simple text file with the contents you need to test.
    2. Create a simple Class that has just a public static void main(String args[]) {...} method.

    In the main method try something like…

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

    From there start adding in your parsing logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.