Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to save a word in a text file

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to save a word in a text file

    This part of the code searches for every word in the text file and saves it to an ArrayList. It searches for the word if its valid of not.

    How can I offer a list of similar words when the word that the user inputs is not in the dictionary. Also I want it to prompt the user to accept the word or enter a replacement that gets saved in the dictionary.

    package spellChecker.project1;
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
     
    public class SpellChecker {
     
    	public static void main(String[] args) {
     
    		Scanner scanner = new Scanner(System.in);
     
    		System.out
    				.println("Enter the path of the dict txt file(C:/Users/Stalin/Desktop/dictionary.txt):");
     
    		String path = scanner.nextLine();
     
    		String myFileName = path;
     
    		int count = -1;
    		int count2 = 0;
     
    		List<String> words = new ArrayList<String>();
     
    		try {
    			FileReader myFile = new FileReader(myFileName);
    			Scanner scanMyFile = new Scanner(myFile);
     
    			while (scanMyFile.hasNext()) {
    				String line = scanMyFile.next();
    				// System.out.println(line);
    				words.add(line);
    				count++;
    				//System.out.println(words.get(count));
    			}
    			scanMyFile.close();
    			// System.out.println(count + " words");
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
    		System.out.println("Please enter a word:");
     
    		String typeword = scanner.next();
     
    		for (int i = 0; i < count; i++) {
    			String word = words.get(i);
    			// System.out.println(word);
    			if (typeword.equals(word)) {
    				System.out.println(typeword + " is a valid word in the dict.");
    			}
    			if (!typeword.equals(word)) {
    				count2++;
    			}
    			if (count == count2) {
    				System.out.println(typeword
    						+ " is an invalid word in the dict.");
    			}
    		}
     
    	}
    }

    thank you guys


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to save a word in a text file

    list of similar words
    That sounds like an interesting project. There must be something on the internet about that type of program.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to save a word in a text file

    yeah I'm searching on the Internet to see if I find anything. What about saving a word that the user inputs to the dictionary file? How can I do this?

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: How to save a word in a text file

    Looks like there are lots of algorithms that you can use to check for similar words. See Check how much a String sounds like another one in Java - Stack Overflow (found using Internet search term "java similar word") for a discussion and suggested algorithms/implementations on this.

    Since the dictionary file is a plain text file, saving a word inputted by a user would involve (re)writing all existing words together with the new word into the same file. See Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O) for details on writing into a file.

Similar Threads

  1. Searching for a word in a text file
    By Miguel08 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: April 17th, 2014, 08:24 PM
  2. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  3. Button to save data in text fields to a File
    By puuts in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 24th, 2013, 10:07 AM
  4. Save array to text file
    By JGW in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2013, 10:45 AM
  5. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM

Tags for this Thread