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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Searching for 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 Searching for a word in a text file

    Hello guys, I'm creating this small JAVA programme thats checks for a word in the dictionary but when I run it I get an error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at spellChecker.project1.SpellChecker.main(SpellCheck er.java:10)"

    This is my code:


    package spellChecker.project1;
     
    import java.io.FileReader;
    import java.util.Scanner;
     
    public class SpellChecker{
      public static void main(String[] args){
     
         String myFileName = "/Users/Stalin/Desktop/dictionary.txt";
         String word= args[0];
         boolean found=false;
         try{
                FileReader myFile = new FileReader(myFileName);
                Scanner scanMyFile = new Scanner(myFile);
                while( scanMyFile.hasNext() ) {
                      String currWord = scanMyFile.next();
                      if(currWord.compareTo(word)==0) {found =true; break;}
    ;
            }
         } catch(Exception ex) { 
               System.out.println("exception "+ex.getMessage()+" caught");
         }
         System.out.println(found); 
      }
    }

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at spellChecker.project1.SpellChecker.main(SpellCheck er.java:10)"
    At line 10 the code uses an index (=0) into an array that is past the end of the array.
    If there is not an element at index 0, that means the array is empty.

    The code should test the length of the array using the .length attribute to make sure there are elements in the array BEFORE trying to index into the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Miguel08 (April 17th, 2014)

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

    Default Re: Searching for a word in a text file

    Thanks for your help. Would the above code work to help me check for a word in a file or offer similar words that the user inputs.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    What happens when the code is executed? Does it do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Miguel08 (April 17th, 2014)

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

    Default Re: Searching for a word in a text file

    No it does not execute I'm still trying to fix that error that you picked up. At what line would i need to test the length of the array using the .length attribute?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    The error message says the error happened at line 10. So the test for the length of the array should be done before line 10.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    I think this code is easier to read but how can I check if the word is contained in the file?


    package spellChecker.project1;


    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;

    public class SpellChecker{

    public static void main(String[] args){


    String myFileName = "/Users/Stalin/Desktop/dictionary.txt";


    try {
    FileReader myFile = new FileReader (myFileName);
    Scanner scanMyFile = new Scanner(myFile);


    while (scanMyFile.hasNextLine()) {
    String line = scanMyFile.nextLine();
    System.out.println(line);
    }
    scanMyFile.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    }
    }

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    how can I check if the word is contained in the file?
    Read the lines from the file into a String and use one of the String class's method to check if the String contains the word.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    How can I do this? sorry if I sound annoying but I'm trying to learn at the same time. If you could show me a small example I think I would understand better.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    Something like this:
    initialize variables
    begin loop to read all lines of file
    read line into String
    check if String contains the word (uses a method of String class)
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    I think Ive done this wrong what it suppose to do is ask the user to enter a name of a file and for every word in that file i need to check if the word is contained in the file.

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    Ok, so you need to read the file, separate out the words and save them in a collection.
    After that you read another file and search each line for one of the words in the collection.

    Do those two steps one at a time. After the words are read into the collection, print it out so you can see if they are all there. For testing use small files with few words say 5 so its easy to sort through the debugging without hugh print outs.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    ok so I read the first file like shown below but how do I separate out the words and save them in a collection ?



     spellChecker.project1;
     
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Scanner;
     
    public class SpellChecker{
     
      public static void main(String[] args){
     
     
    	  String myFileName = "/Users/Stalin/Desktop/dictionary.txt";
     
     
     
    	  try {
    		  FileReader myFile = new FileReader (myFileName);
    		  Scanner scanMyFile = new Scanner(myFile);
     
     
     
     
     
              while (scanMyFile.hasNextLine()) {
                  String line = scanMyFile.nextLine();
                  System.out.println(line);
              }
              scanMyFile.close();
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
     
      }
    }

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    How are the words stored in the file? The String class's split() method will separate the words in a String that are separated by some character like a space or a , (comma) which will put the words into an array from which they can be saved in a collection like an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    The words are stored line by line, the file contains all the words in the dictionary.

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    If there is one word per line you won't need the split() method.
    Read the lines/words and store them in the list.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    How can I read each line into a set<Sting>?

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    Read the line from the file into a String and use one of Set's methods to add the String to the Set.

    Why do you want to put the words into a Set?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    to be honest I don't know what I'm doing Ive tried everything but nothing seems to be working. I've tried to understand how to read the words and store them in the list but I don't know how to do this.

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    nothing seems to be working.
    Does the code read the lines from the file OK?
    For testing, read each line and print it to see if the code works.
    That is the first thing that must work.

    When that works, then move to putting each word in the list.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    Yes the code reads the lines from the file. I'm stuck on the second part I don't know how to code and put each word in the list. Can you show me how this is done please? Thank you

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    how to code and put each word in the list
    The list has some methods that are used to add items to it.
    Define the list variable where all methods can access it at the class level,
    create a new instance of the class and assign it to the variable
    inside the loop where the words are read, use the class's method to add the word to the list
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    When you say list do u mean something like this?

    List<String> list = new ArrayList<String>();

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,585
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Searching for a word in a text file

    Yes that looks like it would be a place to store the words.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Searching for a word in a text file

    Can I load two files like this?


    package spellChecker.project1;
     
     
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.HashSet;
    import java.util.Scanner;
    import java.util.Set;
     
    public class SpellChecker{
     
     
     
     
    	public static void main(String[] args) {
     
     
    		String myFileName = "/Users/Stalin/Desktop/dictionary.txt";
    		String user = "/Users/Stalin/Desktop/user.txt";
     
    		Set<String> set = new HashSet <String>();
     
    		System.out.println("Please enter the location of the file path: ");
    		Scanner UserInput = new Scanner(System.in);
    		user = UserInput.nextLine();
     
    		try{
     
    		 FileReader readMyFile = new FileReader(myFileName);
    		 Scanner scanMyFile = new Scanner(readMyFile);
     
    		 FileReader readMyFile1 = new FileReader(user);
    		 Scanner scanMyFile1 = new Scanner(readMyFile1);
     
     
             while (scanMyFile.hasNextLine()) {
                 String line = scanMyFile.nextLine();
                 //System.out.println(line);
                 set.add(line);
     
             }
     
             while (scanMyFile1.hasNextLine()){
            	 String line = scanMyFile1.nextLine();
            	 set.add(line);
             }
             scanMyFile.close();
             scanMyFile1.close();
             UserInput.close();
    		}
    		catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
     
     
    	}
    }
    Last edited by Miguel08; April 18th, 2014 at 11:00 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Searching a word in a compressed file
    By Insence8 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: December 4th, 2013, 03:45 PM
  2. Searching a word in a compressed file
    By Insence8 in forum Object Oriented Programming
    Replies: 0
    Last Post: December 3rd, 2013, 09:38 AM
  3. [SOLVED] Searching for a word in file
    By justyStepi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2013, 08:39 AM
  4. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 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