Results 1 to 7 of 7
  1. #1
    Subluxation is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Unhappy Need help adding Strings to an Array and writing to file

    Every time i try and add a new word to personalDictionary, it only replaces the first index. In my personalAdd method i had tried to add a running count to advance the position it would be saving in but it doesn't seem to be working correctly... Any advice??

    Java Code:
    import java.util.Arrays;
    import java.util.Scanner;
    import java.io.File;
    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    
    
    public class SpellingChecker 
    {
    	
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		int count = 0;
    		final int SIZE = 100;
    		
    		
    		// The size of the global dictionary is fixed
    		String[] globalDictionary = readDictionary("Dictionary.txt");
    		String[] personalDictionary = new String[SIZE]; //Populate personal dictionary with 100 empty spaces
    		personalDictionary = readDictionary("PersonalDictionary.txt");
    		
    		
    		processInput(globalDictionary,personalDictionary,count);
    	}
    
    	
    	public static void PersonalAdd(String word, String[] personal, int count) throws FileNotFoundException
    	{
    		final int SIZE = 100;
    		if( count<=SIZE)
    			{
    			personal[count] = word;
    			}
    		ArrayToFile(personal,count);
    		++count;
    		return;
    	}
    	
    	
    	public static void ArrayToFile(String[] personal, int count) throws FileNotFoundException
    	{
    		
    		PrintWriter addWord = new PrintWriter(new File ("PersonalDictionary.txt"));
    		addWord.write(java.util.Arrays.toString(personal) + "\n");
    		addWord.close();
    		return;
    			
    	}
    		
    	public static void processInput(String[] global, String[] personal, int count) throws FileNotFoundException
    	{
    		Scanner keyboard = new Scanner(System.in);
    		String answer;
    		
    		// user input will terminate this loop
    		while (true)
    		{
    			// We can't spell check quit--but that's OK.
    			System.out.println("Enter a word or QUIT to stop:");
    			String word = keyboard.next();
    			word = word.toLowerCase(); // the dictionary is in lower case
    			
    			// Get out of this method if the user wants to quit
    			if (word.equals("quit"))
    				return;
    			
    			// Search the global dictionary
    			if (Arrays.binarySearch(global, word) >= 0) //Search global dictionary
    			{
    				System.out.println("That word is spelled correctly");
    				
    			}
    			else if (Arrays.binarySearch(personal, word) >= 0)   //Search personal dictionary
    			{
    				System.out.println("That word is spelled correctly");
    				
    			}
    			else
    			{
    				System.out.println("That word is not spelled correctly");
    				System.out.println("Did you want to add this word to personal dictionary? Yes or No");
    				answer = keyboard.next();
    				if ( answer.equalsIgnoreCase("yes"))
    				{
    					//run method PersonalAdd with word as parameter
    					PersonalAdd(word,personal,count);
    				}
    				
    				
    			
    		} // end while
    		}
    	
    	}
    	
    	
    	public static String[] readDictionary(String fileName) throws FileNotFoundException
    	{
    		Scanner file = new Scanner(new File(fileName));
    		
    		// find the length of the dictionary by reading the file
    		int count = 0;
    		while (file.hasNextLine())
    		{
    			++count;
    			file.nextLine();
    		}
    		file.close();
    		
    		// Now read the file and fill the array
    		String[] result = new String[count];
    		
    		// Reconstruct the file to read it a second time.
    		file = new Scanner(new File(fileName));
    		
    		// Read the file again
    		int index = 0;
    		while (index < count)
    		{
    			result[index] = file.nextLine();
    			++index;
    		}
    		file.close();
    		return result;
    		
    	}
    	
    }
    Last edited by JosAH; 04-10-2015 at 06:25 PM. Reason: added [code] ... [/code] tags

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help adding Strings to an Array and writing to file

    Probably because the count you are incrementing is the method argument variable and not the instance field. So it is always zero in the method. I also recommend you use index instead of count since index indicates the actually place you are writing the value. Count is the number of values. And when count == SIZE you will get an IndexOutOfBoundException.

    You may want to use ArrayLists over Arrays. They are much easier to work with.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Subluxation is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Default Re: Need help adding Strings to an Array and writing to file

    Quote Originally Posted by jim829 View Post
    Probably because the count you are incrementing is the method argument variable and not the instance field. So it is always zero in the method. I also recommend you use index instead of count since index indicates the actually place you are writing the value. Count is the number of values. And when count == SIZE you will get an IndexOutOfBoundException.

    You may want to use ArrayLists over Arrays. They are much easier to work with.

    Regards,
    Jim
    I thought since I passed count through to the other methods it would keep a running total? Do I have to return count to the main program for it to have 1 added to it for every new entry in personalDictionary?

    I would try to work with ArrayLists but my professor "hasn't taught us that" so it cannot be used just yet:/

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help adding Strings to an Array and writing to file

    Quote Originally Posted by Subluxation View Post
    I thought since I passed count through to the other methods it would keep a running total? Do I have to return count to the main program for it to have 1 added to it for every new entry in personalDictionary?
    Most languages (and Java is no exception) pass by value (so only a copy of the instance field is there). In fact, since you are using an instance value, you don't really need to pass anything. Just reference the instance field directly. If there is a conflict, then this.count references the instance field, otherwise, it doesn't matter.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Need help adding Strings to an Array and writing to file

    Instance field? Are we looking at the same code? Its all statics, there is no instance field. Only a local variable in the main().
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help adding Strings to an Array and writing to file

    I looked at it incorrectly (thought the count was an instance field). In any event, local variables go out of scope and don't retain their values when you leave a method (static method or instance method).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Need help adding Strings to an Array and writing to file

    Indeed. We have to keep in mind that this is a student assignment so the correct solution is not the actual correct solution but rather what the teacher expects. In this case probably count SHOULD be a (static) class member.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. writing strings to a file
    By Johnny2009 in forum New To Java
    Replies: 7
    Last Post: 09-21-2012, 09:36 AM
  2. Replies: 5
    Last Post: 05-03-2012, 12:21 AM
  3. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 12:58 AM
  4. Generate and Save Array of Strings to external file
    By thestarncy in forum New To Java
    Replies: 1
    Last Post: 03-05-2010, 04:46 PM

Tags for this Thread

Posting Permissions

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