3 Replies - 2549 Views - Last Post: 24 September 2010 - 05:04 AM Rate Topic: -----

#1 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Word Occurrence

Posted 23 September 2010 - 06:50 PM

I have to prompt for a word and check the # of occurrences of that word in the file. My professor didn't require us to check for punctuation but I would like to add it to my program. I've searched and found a couple of things on it but no short/clear way to do it.
Should I use a for loop and check index by index for the length of the string & if I find something that is not a letter, create a substring of the original with the punctuation removed?
orrrr is their a simpler way?
Any help would be appreciated. Here is my working code (without a punctuation checker).


import java.io.*;
import java.util.*;

public class WordOccurence {
	public static void main(String[] args) {
		try {
		
			// File & keyboard scanners
			Scanner scanner = new Scanner(new File("input.txt"));
			Scanner input = new Scanner(System.in);
			// Counter
			int counter = 0;
			System.out.print("Please enter a word to check number of occurences in file: ");
			String userWord = input.nextLine();
			userWord = userWord.toLowerCase();
			
			// Loop through each line of the file
			while (scanner.hasNext()) {
				String fileWord = scanner.next();
				fileWord = fileWord.toLowerCase();
				if ( userWord.equals(fileWord))
					counter++;
			}
			// Let the user know how many matches were found
			System.out.println("\n" + "Found the word: " + userWord + " " + counter + " times.");
			// Close the file
			scanner.close();
		}
		catch (FileNotFoundException e) {}
	}
}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Word Occurrence

#2 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: Word Occurrence

Posted 23 September 2010 - 07:53 PM

okay so I did a little more research in the API and came up with a solution:

import java.io.*;
import java.util.*;

public class WordOccurrence {
	public static void main(String[] args) {
		try {
			// File & keyboard scanners
			Scanner scanner = new Scanner(new File("input.txt"));
			Scanner input = new Scanner(System.in);
			// Counter
			int counter = 0;
			System.out.print("Please enter a word to check number of occurences in file: ");
			String userWord = input.nextLine();
			userWord = userWord.toLowerCase();
			
			// Loop through each line of the file and tokenize it. 
			while (scanner.hasNext()) {
				String fileWord = scanner.next();
				String temp = fileWord.replaceAll("[^a-zA-Z \n]", "");
				if (userWord.equals(temp))
					counter++;
			}
			// Let the user know how many matches were found
			System.out.println("\n" + "Found the word " + userWord + " " + counter + " times.");
			// Close the file
			scanner.close();
		}
		catch (FileNotFoundException e) { System.out.println(e); }
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Word Occurrence

Posted 24 September 2010 - 03:09 AM

FYI:


String temp = fileWord.replaceAll("[^a-zA-Z \n]", "");


removes all numbers as well. Use /p to check for punctuation.

http://download.orac...ssential/regex/

I haven't compiled your code so I don't know how well it works, but it looks like it should do exactly what you want minus the numbers part.

I might be wrong but I don't think

   String fileWord = scanner.next();
19
                String temp = fileWord.replaceAll("[^a-zA-Z \n]", "");
20
                if (userWord.equals(temp))
21
                    counter++;



works you are getting in a new word then checking the word that you got in against the word that you just cut the punctuation and numbers off of.
Was This Post Helpful? 0
  • +
  • -

#4 ninechances  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 188
  • Joined: 08-February 10

Re: Word Occurrence

Posted 24 September 2010 - 05:04 AM

View Postb0ng01, on 24 September 2010 - 03:09 AM, said:

FYI:


String temp = fileWord.replaceAll("[^a-zA-Z \n]", "");


removes all numbers as well. Use /p to check for punctuation.

http://download.orac...ssential/regex/

I haven't compiled your code so I don't know how well it works, but it looks like it should do exactly what you want minus the numbers part.

I might be wrong but I don't think

   String fileWord = scanner.next();
19
                String temp = fileWord.replaceAll("[^a-zA-Z \n]", "");
20
                if (userWord.equals(temp))
21
                    counter++;



works you are getting in a new word then checking the word that you got in against the word that you just cut the punctuation and numbers off of.


No, my code does exactly what I want - I should have probably made it clear that I only wanted strings that contain letters so it would make sense for me to cut out numbers as well because I don't want to check to see if a word a user enters matches a number or a word with a number thrown in it somewhere. Also, The logic is correct because if I were to check the userWord with fileWord while fileWord still had punctuation in it, I wouldn't actually be counting the word because it would skip it - so I need to create that temporary string to hold the stripped word.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1