CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    1

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

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    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.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured