...

View Full Version : Simple Java (symbol error) problem



SteveNoob
03-24-2012, 03:42 PM
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.







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);
}
}

Fou-Lu
03-24-2012, 05:44 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum