Results 1 to 4 of 4
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Exclamation What is wrong with my code?

    I have written a program to allow a user to enter words to a file DIctionary indefinetly until the end-user selects cancel. However, the words simply won't save to my file. What am I doing wrong??

    Java 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);
    				}
    			}
    		}
    		out.close();
    		aFileWriter.close();
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: What is wrong with my code?

    Are you sure the code is going where you think it should be?
    Stick some System.out.println() statements in there so you can see what route it's taking, and what the value of 'input' is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: What is wrong with my code?

    Denis2k11, please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: What is wrong with my code?

    Check this and you and i am sure that you will understand ;)

    Java 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 == null){
                        cont=true;
                        finished= true;
    				}else if(input.contains(" ")){
                        JOptionPane.showMessageDialog(null,"Please enter one word only!");
                        cont = false;
    				}else{
                        cont = true;
                        out.println(input);
                    }
                }
            }
            out.close();
            aFileWriter.close();
        }
    }

Similar Threads

  1. What is wrong with my code?
    By tweetychirpy in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-23-2011, 06:16 PM
  2. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 12:28 PM
  3. what is wrong with my code???
    By EBBOOO in forum New To Java
    Replies: 30
    Last Post: 07-27-2010, 10:59 PM
  4. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-19-2009, 12:59 AM
  5. what's wrong with my code? please help me...
    By lovely23 in forum Java Applets
    Replies: 2
    Last Post: 03-01-2009, 02:33 PM

Posting Permissions

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