Can someone tell me why does my code not work?
My code is supposed to take in words from the end user and store them in a file called dictionary, one word at a time. But t doesn't do so. Can someone tell me why?
Code:
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
public class RoughWork
{
public static void main(String[]args) throws IOException
{
String maxWords = "", minWords = "";
int maxLength=0, minLength=2000;
String input = "", aWord="";
boolean cont,finished = false;
FileWriter aFileWriter = new FileWriter("Dictionary.txt");
PrintWriter out = new PrintWriter(aFileWriter);
while(!finished)
{
cont = false;
while(!cont)
{
input = JOptionPane.showInputDialog(null,"Please enter the word you'd like to add to the dictionary: ");
if(input.contains(" ") || input.matches(".*\\d.*") ||input == ""){
JOptionPane.showMessageDialog(null,"Please enter one word only!");
cont = false;
}
else if( input == null){
cont=true;
finished= true;
}
else
cont = true;
}
}
out.println(input);
}
}
Re: Can someone tell me why does my code not work?
What is your code failing to do? I see you have created a FileWriter and PrintWriter but never used either to save into the file. Read up on how FileWriter and PrintWriter work since I am guessing your issue is that the file has nothing saved into it.
Essentially what you want to do is
1. Ask user for word
2. If valid input > Save word to file || Else if invalid input take appropriate measures.
3. When finished adding words into the dictionary and outside the loop close the writer.