Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Simple Java (symbol error) problem

    sorry to double post but i am at my wits end trying to understand why my code is not working. I understand that there is very likely some simple problems that need fixing but this i do not need help with.
    When i try to compile this it tells me it cannot find the createNewFile()
    and .exists()
    symbols but i made sure to import them correctly and unless i can no longer see the wood from the trees im stumped.

    A bottle of guinness for the first fellow who can help

    import java.io.*;
    import javax.swing.JOptionPane;
    import java.util.*;
    import java.util.Collections;
    import java.io.File;
    public class DictionaryProject
    	public static int count= 0;
        public static String userInput= "", tempFile, dictFile;
        public static String userMessage ="";
        public static void main (String [] args) throws IOException
        String message1= "1. Add word\n" + "2. Delete word \n" + "3. Search \n" + "4. List on screen dictionary entries\n" + "5. List secondary functions";
        String message2= "1. Add your word now:\n " + "2. Finished creating(Return to main options menu)\n";
        String message3= "1. Delete word now:\n" + "2. Finished destroying (return to main menu)\n";
        String message4= "1. Search for your word now:\n" + "2. Finished fishing (return to main menu)\n";
        String message5= "Display the...\n" + "a. Number of words in dictionary.\n" + "b. Number of words that start with each letter of the alphabet\n" + "c. Number of pallidromes within dictionary\n" + "d. The shortest word\n" + "e. The longest word\n";
        String pattern1= "[0-9, A-Z, a-z] {1,}.txt";
        String pattern2= "1|2|3|4|5";
        String pattern3= "a|b|c|e";
        String temporaryFile, dictionaryFile, result; 
        Vector preDictionaryList = new Vector();
        dictionaryFile = JOptionPane.showInputDialog(null, "Enter the name of your dictionary file now: /n");
        temporaryFile = JOptionPane.showInputDialog(null, "Enter then name of your temporary file now: /n");
        temporaryFile = temporaryFile.toLowerCase();
        dictionaryFile = dictionaryFile.toLowerCase();
        if (!(dictionaryFile.matches(pattern1) || temporaryFile.matches(pattern1)))
            result = "The file name entered does not match the required criteria, please ensure your filenames are spelt correctly.";
    	        File dictFile = new File (dictionaryFile);
                File tempFile = new File (temporaryFile);
             if( !(dictFile.exists() || tempFile.exists()))
    	     result = "File does not exist, Creating file now";
    	     FileWriter fw1 =new FileWriter(dictFile);
    	     FileWriter fw2 =new FileWriter(tempFile);
    	     BufferedWriter bw1 = new BufferedWriter(fw1);
    	     BufferedWriter bw2 = new BufferedWriter(fw2);
    	         FileWriter fw1 =new FileWriter(dictFile);
    	         FileWriter fw2 =new FileWriter(tempFile);
    	         BufferedWriter bw1 = new BufferedWriter(fw1);
    	         BufferedWriter bw2 = new BufferedWriter(fw2);

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    This is a scope issue. When you are in the else, File declared and will not be available outside of the else.
    Move the entire if and else block for the exists check into the else clause of the .matches check. If the match for the file fails, then the entire lower section shouldn't execute at all. The alternative is to declare the File objects before any of the if/else calls, and add a != null check to the exists block (since now there is no guarantee that File has been constructed). I'd suggest moving it into the else since its just easier to move one bracket down.

  3. Users who have thanked Fou-Lu for this post:

    SteveNoob (03-24-2012)


Posting Permissions

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