Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-24-2012, 03:42 PM   PM User | #1
SteveNoob
New to the CF scene

 
Join Date: Mar 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
SteveNoob is an unknown quantity at this point
Simple Java (symbol error) problem

Hey
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
Thanks,
S.




Code:
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.";
    }
       else
       {
	        File dictFile = new File (dictionaryFile);
            File tempFile = new File (temporaryFile);
       } 
         if( !(dictFile.exists() || tempFile.exists()))
         {
	     result = "File does not exist, Creating file now";
	     dictFile.createNewFile();
	     tempFile.createNewFile();
	     FileWriter fw1 =new FileWriter(dictFile);
	     FileWriter fw2 =new FileWriter(tempFile);
	     BufferedWriter bw1 = new BufferedWriter(fw1);
	     BufferedWriter bw2 = new BufferedWriter(fw2);
         }
         else 
         {
	         FileWriter fw1 =new FileWriter(dictFile);
	         FileWriter fw2 =new FileWriter(tempFile);
	         BufferedWriter bw1 = new BufferedWriter(fw1);
	         BufferedWriter bw2 = new BufferedWriter(fw2);
     }
     }
SteveNoob is offline   Reply With Quote
Old 03-24-2012, 05:44 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
SteveNoob (03-24-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:51 AM.


Advertisement
Log in to turn off these ads.