Results 1 to 3 of 3
  1. #1
    chiguy is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default FileScanner Question

    Hi Everyone.

    I am working on a file expansion program that adds files together

    Basically the format of the file is:

    some text

    #include "anotherfile.txt"

    and I have to add the files together and produce one output file.

    My program does this fine- BUT - it needs to ignore any "#include" directive that is NOT at the beginning of a line.

    Is there a way to accomplish this? Here is my code:
    while (scan.hasNext())
    {
    temp = scan.next();
    // If it finds another "#include" indicator, it reads the
    // next filename and calls itself.

    if (temp.equals("#include"))
    {
    fname = scan.next();
    if (fname.charAt(0)!= '"')
    {
    System.out.println("No quoted filename.");
    System.exit(0);
    }
    fname = fname.substring(1,fname.length()-1);

    if (calledfiles.contains(fname))
    {
    System.out.println("Infinite Loop Detected- Aborting.");
    System.exit(0);
    }
    try
    {
    scan2 = fileScanner(fname);
    }
    catch(FileNotFoundException e)
    {
    System.out.println("File Read Error");
    System.exit(0);
    }
    // ReadIncluded is the function that contains this code- it is recursive
    ReadIncluded(scan2,fname);
    if (scan.hasNextLine())
    {
    scan.nextLine();
    }
    }
    else
    {
    // file is an array that contains the completed file. any text that is not #include or a filename is added here.
    file.add(temp);
    }
    Thank you!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Rather than use Scanner#next() how about using Scanner#nextLine(), and then use a specific String method to see if the current temp String starts with your key String.

    Oh, by the way, welcome to the forum! A suggestion, when posting code, don't use quote tags, but rather use code tags.

    This is easier to read:
    Java Code:
       public MyGui(MyModel mymodel) {
          this.model = mymodel;
          
          JButton addBtn = new JButton("Add");
          JButton subtractBtn = new JButton("Subtract");
          addBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                addActionPerformed();
             }
          });
          subtractBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                subtractActionPerformed();
             }
          });

    Than this:
    public MyGui(MyModel mymodel) {
    this.model = mymodel;

    JButton addBtn = new JButton("Add");
    JButton subtractBtn = new JButton("Subtract");
    addBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    addActionPerformed();
    }
    });
    subtractBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    subtractActionPerformed();
    }
    });
    Much luck!

  3. #3
    chiguy is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    Thanks!

    I thought there might be an easier way but I implemented the nextLine and works perfectly.

    I'll use the code tags in the future, sorry :P

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Question about what this do.
    By Syfer in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 08:35 AM
  3. Question about JMF
    By Supamagier in forum Advanced Java
    Replies: 0
    Last Post: 05-23-2009, 11:04 AM
  4. Question!
    By anjogomez in forum Java Applets
    Replies: 2
    Last Post: 02-21-2009, 03:24 AM
  5. JNI question
    By javaplus in forum New To Java
    Replies: 0
    Last Post: 12-24-2007, 10:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •